2017-10-27 17 views
0

eclipseでJSPを使用してWebアプリケーションを作成しました。ログインページと他のページがあります。デプロイして他のページにアクセスしようとすると、ログインページを使用してログインせずにアクセスできます。だから私は、ログインページを介してログインする前に他のページのURLにアクセスしようとするたびにログインページにリダイレクトしたい。どうやってするか?誰かが私にこれを手伝っていただければ幸いです。jspログインページにリダイレクトする方法、ユーザーがまだログインしていない場合、Javaサーブレット?

おかげ

答えて

1

あなたも探すことができ:Click here。すべてのページで、ユーザーの資格情報(ログインユーザー名またはログインパスワード)の詳細を確認するロジックを実装する必要があります。詳細が存在する場合はそれを処理し、それ以外の場合はエラーページにリダイレクトします。今のところ、ログイン後にログイン時に属性を設定するロジックを実装しました。セッションにエラーメッセージを設定しないと、すべてのデータをセッションに設定します。私はセッションに2つの属性を追加したのはあなたの知識のため、通常は私たちはセッションにUserClassを格納します。

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> 

希望これはあなたの幸運のために有用です。他の詳細が必要な場合はお知らせください

0

web.xmlファイルに次のコードを挿入してください。

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

アプリケーションを実行すると、最初のページがlogin.jspとして表示されます。つまり、認証が継続されます。

関連する問題