@SuppressWarnings({"serial"})
public class LoginServlet extends HttpServlet {
/**
* Constructor of the object.
*/
public LoginServlet() {
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 {
response.setContentType("text/html");
request.setCharacterEncoding("UTF-8");
String userName = request.getParameter("userName");
String password = request.getParameter("password");
ServletContext sc = request.getServletContext();
if(userName == null || password == null){
response.sendRedirect(request.getContextPath() + "/index.jsp");
return;
}
UserService userService = WebApplicationContextUtils.getWebApplicationContext(sc).getBean(UserService.class);
User user = userService.findByUserNameAndPassword(userName, password);
if(user!=null){
System.out.println("登陆成功!");
request.getSession().setAttribute("user", user);
response.sendRedirect(request.getContextPath() + "/pages/index/index.zul");
}else{
response.sendRedirect(request.getContextPath() + "/index.jsp");
}
}
/**
* Initialization of the servlet.
*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
// Put your code here
}
}
登陆 filter:
public class LoginFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void doFilter(ServletRequest servletrRequest, ServletResponse servletrResponse,
FilterChain chain) throws IOException, ServletException {
// TODO Auto-generated method stub
HttpServletRequest request = (HttpServletRequest)servletrRequest;
HttpServletResponse response = (HttpServletResponse)servletrResponse;
User us = (User)request.getSession().getAttribute("user");
if (us == null) {
response.sendRedirect(request.getContextPath() + "/index.jsp");
return;
}else{
chain.doFilter(servletrRequest, servletrResponse);
}
}
@Override
public void destroy() {
// TODO Auto-generated method stub
}
WEB.XML:
OpenSessionInViewFilter
org.springframework.orm.hibernate4.support.OpenSessionInViewFilter
OpenSessionInViewFilter
/*
log4jConfigLocation
/WEB-INF/classes/log4j.properties
log4jRefreshInterval
6000
Log4j Loader Listener
org.springframework.web.util.Log4jConfigListener
contextConfigLocation
classpath:spring-*.xml
Spring Context Loader Listener
org.springframework.web.context.ContextLoaderListener
LoginServlet
com.joytrav.emp.web.servlet.LoginServlet
LoginServlet
/doLogin
Login Filter
com.filters.LoginFilter
Login Filter
/pages/*
Logoff
com.servlet.Logoff
Logoff
/emp/logoff
index.html
index.htm
index.jsp
default.html
default.htm
default.jsp
解决方案1: session过期或者重启tomcat后sesseion里当然没有值啊
以上介绍了“ javaWEB登陆功能,待session消失和重启tomcat后报重定向异常”的问题解答,希望对有需要的网友有所帮助。
本文网址链接:http://www.codes51.com/itwd/2078768.html
相关图片
相关文章