首页 >Java >java教程 > 用Java实现网页登录和注册

用Java实现网页登录和注册

一个新手
一个新手原创
2017-09-07 09:26:206175浏览

1.写网页有html,jsp,servlet.但是三者有各自的优缺点,html适合写一些静态的显示,jsp适合写动态可变的显示,servlet适合处理业务逻辑,分发转向,DAO数据中转。

2.本项目使用jsp+servlet来实现一个简单的网页注册和登录,需要一点jsp的语法基础,比如9c1b7e15dedb80b6af8395013997b329、5b80f63fa6b362342da217a1bc06c7f85db79b134e9f6b82c0b36e0489ee08ed表示转发。form跟html一样表示表单。117befdeba1fd957497e82f8445cb3d1表示注释。
a51d2e2da6e7069c0f0d8111919fc496申明全局变量等基础的语法,还有几个指令page include taglib

3.建立一个web网页,默认生成一个web.xml文件。工程名为webServletTest  在java Resouces目录下建立一个webservlet.java。

package webJspDemo.com;import java.io.IOException;import javax.servlet.ServletException;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;/**
 * Servlet implementation class LoginServlet
 */@WebServlet("/servlet/loginservlet")public class LoginServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;    /**
     * @see HttpServlet#HttpServlet()
     */
    public LoginServlet() {        super();        // TODO Auto-generated constructor stub
    }    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {        // TODO Auto-generated method stub
        response.getWriter().append("Served at: ").append(request.getContextPath());        //获取表单数据
        response.setContentType("text/html; charset=UTF-8");
        request.setCharacterEncoding("UTF-8");
        String userName = request.getParameter("userName");
        String pwd = request.getParameter("pwd");        if("tom".equals(userName)&&"123".equals(pwd)){
            request.getSession().setAttribute("name", userName);
            request.getRequestDispatcher("/register.jsp").forward(request, response);
        }else{
            request.setAttribute("msg", "用户名或者密码不正确");
            request.getRequestDispatcher("/login.jsp").forward(request, response);
        }        //分发转向
    }    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {        // TODO Auto-generated method stub
        doGet(request, response);
    }

}

4.修改web.xml的映射关系和类名

  <servlet>
    <servlet-name>LoginServlet</servlet-name>
    <servlet-class>webJspDemo.com.LoginServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>LoginServlet</servlet-name>
    <url-pattern>/login</url-pattern>
  </servlet-mapping>

5.在WEB-INF目录下建立3个jsp,分别为home.jsp、login.jsp、register.jsp文件。

home.jsp代码为

<body>
    <h1>欢迎来到本站!</h1>
    <%        
    String userName =(String)session.getAttribute("name");
    out.print(userName);
    %>
</body>

login.jsp代码为:

<body>
    <%        
    String msg = (String)request.getAttribute("msg");      
    if(msg !=null){
          out.print(msg);     
      }

    %>
    <form action="/webJspDemo1/servlet/loginservlet" method="get">
        用户名:<input type="text" name="userName"/><br/>
        密码:<input type="password" name="pwd"/><br/>
        <input type="submit" value="登录"/><br/>

    </form></body>

register.jsp代码为:

<body>
    欢迎你登录:    <% 
        String userName =(String)session.getAttribute("name");
        out.print(userName);        
    %>
    <a href="/webJspDemo1/home.jsp">跳到主页 </a></body>

5.实验结果
在浏览器中输入:http://localhost:8080/webJspDemo1/login
在表单中输入tom和123之后就会跳转到register.jsp界面,然后在到home.jsp界面,基本上就完成一个简单的注册流程。


以上是 用Java实现网页登录和注册 的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn