import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class session extends HttpServlet {
/**
* Constructor of the object.
*/
public session() {
super();
}
/**
* Destruction of the servlet.
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
/**
* The doGet method of the servlet.
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
/**
* The doPost method of the servlet.
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String username = "";
String password = "";
username = request.getParameter("username");
password = request.getParameter("password");
HttpSession session = request.getSession();
session.setAttribute("username", username);
session.setAttribute("password", password);
request.setAttribute("name", username);
request.setAttribute("pwd", password);
RequestDispatcher dis = request.getRequestDispatcher("/getsession.jsp");
dis.forward(request, response);
/*
response.sendRedirect("https://localhost:8080/sessiontest/getsession.jsp");
*/
//这个路径必须是这样写,而不能像上面的request.getRequestDispatcher那样使用相对路径
// 而且要是使用response.sendRedirect的话在下面的session.jsp中不能通过request.getAttribute来获取request对象
//因为前后使用的不是同一个request,但是session可以,因为session会一直存在直到用户关闭浏览器
}
/**
* Initialization of the servlet.
*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
// Put your code here
}
}