session练习
用户名和密码登录,然后输出
登录界面 <%@ 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> <form action="session0627.jsp" method="post"> <table> <tr> <td>用户名:</td> <td><input type="text" name="username"/></td> </tr> <tr> <td>密码:</td> <td><input type="password" name="password"/></td> </tr> <tr> <td><input type="submit" value="登录"/></td> </tr> </table> </form> </body> </html>
设置session <%@ 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> <% String username=new String(request.getParameter("username").getBytes("ISO-8859-1"),"UTF-8"); session.setAttribute("name", username); String password=request.getParameter("password"); session.setAttribute("mima", password); %> 登录成功 </body> </html>
显示用户名和密码 <%@ 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> 您的用户信息为:<br> <% String str=session.getAttribute("name").toString(); String str1=session.getAttribute("mima").toString(); out.print("用户名:"+str+"<br>"); out.print("密码:"+str1); %> </body> </html>
运行结果: