index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <title>用户登录</title> </head> <body> <div style="width:600px; margin:40px auto; line-height:40px;"> <h3>    用户登录</h3> <form action="loginCheck.jsp" method="post"> <!-- 可尝试:action="headerOfRequest.jsp" --> 用户名:<input type="text" name="username"> tom或张三 <br> 密 码:<input type="password" name="password"> 1 <br>      <input type="submit" value="提交"> </form> </div> </body> </html>
main.jsp
<%@page import="java.net.URLDecoder"%> <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <title>用户功能</title> </head> <body> <div style="width:600px; margin:40px auto; line-height:40px;"> <h3>    用户功能</h3> <% request.setCharacterEncoding("UTF-8"); //设定编码,否则输入的中文成乱码。须在获取输入值之前设定 String msg = ""; //存放消息 String username = request.getParameter("username"); //获取输入的值。若不是从表单提交而来,则为null String password = request.getParameter("password"); msg = request.getParameter("msg"); //获取URL参数的值 System.out.println("解码之前:" + msg); if (msg != null) { msg = URLDecoder.decode(msg, "UTF-8"); //URL解码。对于URL参数值可省略解码 System.out.println("解码之后:" + msg); } %>   <span style="color:red; font-size:small;"><%= msg %></span> <br>用户名:<%= username %> <br>密 码:<%= password %> <br><br>      <a href="index.jsp">用户登录</a> </div> </body> </html>
loginCheck.jsp
<%@page import="java.net.URLEncoder"%> <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <title>登录验证</title> </head> <body> <div style="width: 600px; margin: 40px auto; line-height: 40px;"> <h3>    登录验证</h3> <% request.setCharacterEncoding("UTF-8"); //设定编码,否则输入的中文成乱码。须在获取输入值之前设定 String msg = ""; //存放消息 String username = request.getParameter("username"); //获取输入的值 String password = request.getParameter("password"); for (int f = 0; f < 1; f++) { //为了方便随时退出循环去执行本循环后面的语句 if (username == null || password == null) { //若控件名错误,或不是从表单提交而来,则为null msg = "未正确提交表单!"; break; //不符合要求,退出循环 } username = username.trim(); //去除首尾空格 if ((username.equals("tom") == false || password.equals("1") == false) && (username.equals("张三") == false || password.equals("1") == false)) { msg = "用户名或密码错误!"; //用户名或密码错误,两套都不满足 break; } msg = username + "登录成功!"; System.out.println("编码之前:" + msg); //输出到控制台Console msg = URLEncoder.encode(msg, "UTF-8"); //URL转码。对于URL中的参数值 System.out.println("编码之后:" + msg); String url = "main.jsp?msg=" + msg; //URL参数msg的值是变量msg的值 if (username.equals("tom")) { response.sendRedirect(url); //重定向(通过浏览器重新发起跳转请求) return; //结束当前页面,不再继续执行本页的代码 所以用户名和密码对的情况下,最后显示 // 红色信息 tom登录成功! 用户名:null 密码:null } if (username.equals("张三")) { //转发。在服务器端直接跳转并转发request和response request.getRequestDispatcher(url).forward(request, response); return; } // if (username.equals("tom")) { // request.getRequestDispatcher(url).forward(request, response); // return; // } //所以用户名和密码对的情况下,最后显示 // 红色信息 tom登录成功! 用户名:tom 密码:1 } %>   <span style="color: red; font-size: small;"><%=msg%></span> <br>用户名:<%=username%> <br>密 码:<%=password%> <br> <br>      <a href="index.jsp">用户登录</a> </div> </body> </html>
jsp_ch3.5_login
最新推荐文章于 2024-07-23 21:45:09 发布