简体   繁体   English

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

[英]How to redirect to jsp login page, if user does not logged in already, Java servlet?

I have wrote an web application using JSP in eclipse. 我已经在Eclipse中使用JSP编写了一个Web应用程序。 It has login page and other pages. 它具有登录页面和其他页面。 After i deployed and trying to access the other pages, i am able to access without login using login page. 部署并尝试访问其他页面后,无需登录即可使用登录页面访问。 So i want to redirect to login page whenever user trying to access other pages url before logged in through login page already. 因此,只要用户尝试通过登录页面登录之前尝试访问其他页面的URL,我想重定向到登录页面。 How to do that? 怎么做? If anyone help me on this one will be appreciated. 如果有人在这方面帮助我,将不胜感激。

Thanks 谢谢

You can even look for : Click here . 您甚至可以寻找: 单击此处 You have to implement a logic which checks user credentials (login username or login password) details on every page . 您必须实现一种逻辑,该逻辑检查每个页面上的用户凭据(登录用户名或登录密码)详细信息。 if details present then process it further else redirect to Error page . 如果存在详细信息,则进行进一步处理,否则重定向到错误页面。 for now i have implemented a logic which set attribute on login after login it sets all the data into session if not set error message into session . 现在我已经实现了一种逻辑,该逻辑在登录后设置登录时的属性,如果未将错误消息设置为session,则它将所有数据设置为session。 usually we will store UserClass into session since it is for your knowledge that's why i have added two attribute into session . 通常我们会将UserClass存储到session中,因为据您所知,这就是我在session中添加了两个属性的原因。

LoginServlet.java 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 : 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 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>

Hope this is helpful for you Good luck . 希望这对您有所帮助。 let me know if you want other details too 让我知道您是否还需要其他详细信息

Put the following code in your web.xml file. 将以下代码放入您的web.xml文件中。

 <welcome-file-list>
        <welcome-file>login.jsp</welcome-file>
 </welcome-file-list> 

So that when run the application it will show the first page as login.jsp. 这样,在运行该应用程序时,它将第一页显示为login.jsp。 Which means the authentication will go on. 这意味着身份验证将继续。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 React MongoDB 电子商务网站。 如果用户已经登录,如何从“帐户”按钮重定向到用户信息而不是登录页面? - React MongoDB E-Commerce website. If user is already logged in, how to redirect from "Account" button to User info instead of Login page? 如果已经使用 Reactjs 登录,如何限制用户显示登录页面 - How to restrict user to display login page if already logged in using Reactjs JSP Servlet 中的登录页面 - Login page in JSP Servlet 如果用户已经登录,则将用户重定向到受保护的路由会导致登录页面在重定向前重新呈现并显示一段时间 - Redirect user to protected route if user already logged in causes login page to re-render and display for sometime before redirection 使用php将登录用户从登录页面重定向到用户仪表板 - Redirect a logged in user from login page to user dashboard with php 如果未登录,则将用户从 JQuery 自动完成重定向到登录页面 - Redirect user to login page from JQuery autocomplete if not logged in 如果用户尚未登录Angular JS,则重定向到登录页面 - Redirect to a login page if the user is not yet logged in Angular JS 如何将用户重定向到登录页面 - how to redirect user to login page with 如何防止登录用户看到登录页面 - How to prevent logged in user to see login page 如果未使用AngularFire 2登录,则重定向到登录页面 - Redirect to login page, if not logged in with AngularFire 2
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM