jsp+Servlet创建简单登陆页面

    xiaoxiao2023-10-16  32

    登陆页面的主要功能->

    输入正确信息时跳转到welcome.jsp,并且浏览器保存正确信息 输入错误信息时显示“用户名或密码错误” . 再次登录时系统会显示保存的信息 .

    4.未登录直接访问welcome.jsp时会提示请先登录 大体思路 ->

    输入信息正确时,使用 response.sendRedirect重定向到welcome.jsp。错误时,建立一个err,赋值“用户名或密码错误”,然后request.getRequestDispatcher请求转发到login.jsp。注:request.getRequestDispatcher()是请求转发,前后页面共享一个request , 这个是在服务端运行的(不会跳转),对浏览器来说是透明的;response.sendRedirect()是重新定向,前后页面不是一个request。(跳转到其他页面)而这个是在浏览器端运行的使用cookie语句来保存用户信息,并设置保存时间为1天。向session中存放登录信息,当访问welcome.jsp时,如果uname存在,则欢迎,否则重新登录。

    login.jsp:

    <%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>。。登录。。</title> </head> <body> <h1>登录</h1> <% String err = (String) request.getAttribute("err"); out.print(err==null?"":err); %> <% String uname=null; String upwd=null; Cookie cookies[]=request.getCookies(); for(Cookie co:cookies){ if(co.getName().equals("uname")){ uname=co.getValue(); } if(co.getName().equals("upwd")){ upwd=co.getValue(); } } %> <form action="/practice/LoginServlet" method="post"> 用户名:<input type="text" name="uname" value=<%=(uname==null?"":uname) %>><br> <%--话说三元运算不太懂。value后的意思:uname是否为空,不是的话输出uname的值,是的话就输出空字符串 --%>&nbsp;&nbsp;&nbsp;码:<input type="password" name="upwd" value=<%=(upwd==null?"":upwd) %> ><br> <input type="submit" value="登录"> </form> </body> </html>

    welcome.jsp:

    <%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>。。登录。。</title> </head> <body> <h1>登录</h1> <% String err = (String) request.getAttribute("err"); out.print(err==null?"":err); %> <% String uname=null; String upwd=null; Cookie cookies[]=request.getCookies(); for(Cookie co:cookies){ if(co.getName().equals("uname")){ uname=co.getValue(); } if(co.getName().equals("upwd")){ upwd=co.getValue(); } } %> <form action="/practice/LoginServlet" method="post"> 用户名:<input type="text" name="uname" value=<%=(uname==null?"":uname) %>><br>&nbsp;&nbsp;&nbsp;码:<input type="password" name="upwd" value=<%=(upwd==null?"":upwd) %> ><br> <input type="submit" value="登录"> </form> </body> </html>

    LoginServlet:

    package Login; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; @WebServlet("/LoginServlet") public class LoginServlet extends HttpServlet { private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("utf-8"); String name = request.getParameter("uname"); String pwd=request.getParameter("upwd"); if(name.equals("Chy") && pwd.equals("111")){ HttpSession session = request.getSession();//向session中存放登录信息 Cookie cookie=new Cookie("uname",name); Cookie cookiee=new Cookie("upwd",pwd); response.addCookie(cookie); response.addCookie(cookiee); cookie.setMaxAge(60*60*24); cookiee.setMaxAge(60*60*24);//设置保存时间为1天 session.setAttribute("uname", name); session.setAttribute("upwd",pwd); response.sendRedirect("/practice/welcome.jsp");//重定向到welcome.jsp }else{ request.setAttribute("err", "用户名或密码错误"); request.getRequestDispatcher("/login.jsp").forward(request, response);//请求转发到login.jsp } } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
    最新回复(0)