JSP登录中Session的用法实例详解(jsp实现用户登录注册)学会了吗

随心笔谈11个月前发布 admin
89 0


import java.io.IOException;
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;

@WebServlet(“/IndexServlet”)
public class IndexServlet extends HttpServlet {
private static final long serialVersionUID=1L;

public IndexServlet() {
super();
// TODO Auto-generated constructor stub
}

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.getWriter().append(“Served at: “).append(request.getContextPath());
}

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
request.setCharacterEncoding(“utf-8”);
String user=request.getParameter(“user”);
String password=request.getParameter(“password”);

String path=request.getContextPath();
HttpSession session=request.getSession();

if (“1”.equals(user) && “1”.equals(password)) {

session.setAttribute(“name”, user);
response.sendRedirect(path + “/success.jsp”);

}else{
response.sendRedirect(path + “/Index.jsp”);
}
}

}

© 版权声明

相关文章