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>Insert title here</title> </head> <body> <form action="dologin.jsp" method = "post"> 姓名:<input type="text" name="uname"/><br/> 密码:<input type="password" name="upwd"/><br/> <input type="submit" value="登录"/><br/> </form> </body> </html>
dologin.jsp界面:
<%@page import="javax.websocket.Session"%> <%@page import="java.nio.channels.SeekableByteChannel"%> <%@ 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>Insert title here</title> </head> <body> <% request.setCharacterEncoding("utf-8"); String name =request.getParameter("uname"); String pwd = request.getParameter("upwd"); if(name.equals("zs")&&pwd.equals("123")){ session.setAttribute("uname", name); session.setAttribute("upwd", pwd); request.getRequestDispatcher("Welcome.jsp").forward(request, response); }else{ response.sendRedirect("login.jsp"); } %> </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>Insert title here</title> </head> <body> 欢迎您: <% String name = (String)session.getAttribute("uname"); out.print(name); %> </body> </html>