javascript - 如果用户尚未登录,Java servlet如何重定向到jsp登录页面?

标签 javascript java web-services jsp servlets

我在 eclipse 中使用 JSP 编写了一个 Web 应用程序。它有登录页面和其他页面。在我部署并尝试访问其他页面后,我可以使用登录页面无需登录即可访问。因此,每当用户在通过登录页面登录之前尝试访问其他页面 url 时,我想重定向到登录页面。怎么做?如果有人在这方面帮助我,我将不胜感激。

谢谢

最佳答案

您甚至可以查找:Click here 。您必须实现一个逻辑来检查每个页面上的用户凭据(登录用户名或登录密码)详细信息。如果存在详细信息,则进一步处理,否则重定向到错误页面。现在我已经实现了一个逻辑,该逻辑在登录后设置登录属性,如果未将错误消息设置到 session 中,它将所有数据设置到 session 中。通常我们会将 UserClass 存储到 session 中,因为这是为了您的知识,这就是我在 session 中添加两个属性的原因。

LoginServlet.java

package com.servlets;

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("/LoginServlet")
public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;

public LoginServlet() {
    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());
}

/**
 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
 */
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
    String uname=request.getParameter("uname");
    String pword=request.getParameter("pword");
    if(null!=uname && uname!="" && pword!=null && pword!=""){
        HttpSession  session=request.getSession(true);
        session.setAttribute("uname", uname);
        session.setAttribute("pword", pword);
        response.getWriter().append("Login SucessFully");
    }else{
          response.sendRedirect("index.jsp"); //error
          HttpSession  session=request.getSession(true);
            session.setAttribute("errorMessage", "Login Failed ");

    }
    //doGet(request, response);
}

}

index.jsp:

  <!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=windows-1256">
<title>Login Page</title>
</head>
<%
String errorMessage = (String) session.getAttribute("errorMessage");
if (null !=errorMessage) { %>
<h4> <%=errorMessage %></h4>
<%}
%>
<body>
<form action="LoginServlet"  method="post" >
    Please enter your username <input type="text" name="uname" id="uname" />
 <br>
    Please enter your password <input type="text" name="pword" id="pword" />
    <input type="submit" value="submit">
 </form>
 </body>
</html>

PostLogin.jsp

 <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
  <!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=ISO-8859-1">
 <title>Simple Page which can't be access without Login</title>
 </head>
  <%
    String uname = (String) session.getAttribute("uname");
    if (null == uname) {
    session.setAttribute("errorMessage", "Login Failed ");
    response.sendRedirect("userLogged.jsp");
    }
 %>
 <body>
 <h4>Simple Page which can be access without Login </h4>
 </body>
 </html>

希望这对您有帮助,祝您好运。如果您还需要其他详细信息,请告诉我

关于javascript - 如果用户尚未登录,Java servlet如何重定向到jsp登录页面?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46966452/

相关文章:

java - org.hibernate.MappingException : Could not determine t ype for: com. yammer.dropwizard.tailor.model.CustomerModel

javascript - 查询 SELECT * AS 不起作用

javascript - 如何在 jsreport 的图表中使用自定义数据?

java - 消息队列提供事务支持吗?

java - 数组List中的空指针异常

javascript - 使用 ISO V2 Coated 等颜色配置文件将 CMYK 颜色转换为 RGB?

javascript - Angular js自定义过滤器未定义

javascript - Ajax 在等待 d3 graphs/.json 响应时加载图形? ( rails )

java - 在java6中向接口(interface)添加新方法而不影响该接口(interface)的现有子类的方法?

java - 使用 JSON 和 Xstream