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>
<script type="text/javascript" src="../js/jquery-1.7.2.js"></script>
<script type="text/javascript">
$(function(){
$(":button").click(function(){
document.getElementsByTagName("form")[0].submit();
}); })
</script>
<body>
<form action="../loginSrevlet" method="post">
用户名:<input type="text" name="userName"/><br/>
密码:<input type="password" name="password"/><br/>
<button type="button">登录</button>
<button type="reset">重置</button>
</form>
</body>
</html>
 package com.zdsofe.servlet1;

 import java.io.IOException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List; import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession; /**
* Servlet implementation class loginSrevlet
*/
@WebServlet("/loginSrevlet")
public class loginSrevlet extends HttpServlet {
private static final long serialVersionUID = 1L; /**
* @see HttpServlet#HttpServlet()
*/
public loginSrevlet() {
super();
// TODO Auto-generated constructor stub
} /**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
} /**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//编码格式
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
response.setContentType("text/heml charset=utf-8"); //得到表单的数据
Enumeration<String > name=request.getParameterNames();
List<String> list=new ArrayList<String>();
while(name.hasMoreElements())
{
list.add(request.getParameter(name.nextElement()));
} //创建Session对象
HttpSession ss= request.getSession();
ss.setAttribute("uName", list.get(0));
response.sendRedirect("/webProject1/jsp/loginWelcome.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">
<!-- 实现3秒后跳转功能-->
<!-- <meta content="3;url=loginBottom.jsp" http-equiv="refresh"> -->
<title>Insert title here</title>
</head>
<body>
<%=session.getAttribute("uName") %>
<button onclick="location.href='loginBottom.jsp'">跳转</button>
三秒后跳转....
</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>
Welcome!
</body>
</html>
上一篇:HTML5:一个拖拽网页元素的例子


下一篇:ubuntu 16.04 php 安装curl方法