jsp 运用 session 登录输出

1、登录页面

<%@ 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="lianxi1.jsp" method="post">
用户名:<input type="text" name="name"><br>
密&nbsp&nbsp&nbsp&nbsp码:<input type="password" name="password"><br>
<input type="submit" value="登录">
</form>
</body>
</html>

 运行并添加数据

2、把数据添加到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>Insert title here</title>
</head>
<body>
<%
String name=new String(request.getParameter("name").getBytes("ISO-8859-1"),"UTF-8");
String password=request.getParameter("password");
session.setAttribute("name", name);
session.setAttribute("pword", password);
%>
添加session成功
</body>
</html>

 

3、输出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>Insert title here</title>
</head>
<body>
<%
String str=session.getAttribute("name").toString();
out.print("name="+str);
String s=session.getAttribute("pword").toString();
out.print("<br>password="+s);
%>
</body>
</html>

 运行结果:

 

posted @ 2016-06-27 16:44  jakermk  阅读(907)  评论(0编辑  收藏  举报