2

私はプログラミングにかなり慣れていないので、ログアウトに関するガイダンスが必要です。バックエンド サーバーで動作する Web ベースのアプリケーションを作成しています。ログインは正常に機能していますが、ログアウトの実装方法がわかりません。どんな助けでも大歓迎です。ログイン用のサーブレット コードは次のとおりです。

protected void processRequest(HttpServletRequest request
                    , HttpServletResponse response)
        throws ServletException, IOException {

    String address = "";
    String operation = request.getParameter("operation");
    //  System.out.println(operation);
    HttpSession session = request.getSession(true);

    UserModel u = null;
    if (operation != null) {

        if (operation.equalsIgnoreCase("login")) {

            u = new UserModel();

            String Username = request.getParameter("username");
            String Password = request.getParameter("password");
            System.out.println("userModel" + u);
            u = u.find(Username, Password);
            System.out.println("i" + u.getUsername());

            if (u.getUsername() != null) {

                session.setAttribute("user", u);
                address = "Homepage.jsp";
            } else {
                address = "InvalidLogin.jsp";
            }

そして、これが私のJSPページコードです:

<form id="login1" action="Servlet">
    <input type="hidden" name="operation" value="login" />
    <h1>Log In</h1>
    <fieldset id="inputs">
        <input id="username" type="text" placeholder="Username" 
               name="username"autofocus required>   
        <input id="password" type="password" placeholder="Password"
               name="password" required>
    </fieldset>
    <fieldset id="actions">
        <button type="submitm"  value="Login" 
               class="IR" id="IRButton"><em></em></button>
    </fieldset>
</form>
<form action="Register1.jsp">
    <button type ="submitn" value="Register" class="IR2"
               id="IRButton2"><em></em></button>
</form>

....両方のページで必要なコードを誰か教えてくれたら、それは素晴らしいことです!ありがとう

4

1 に答える 1

1

これを試してください...Session Managementログインとログアウトのアクションについても検討してください

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
    <servlet>
        <servlet-name>ServletLogic</servlet-name>
        <servlet-class>model.ServletLogic</servlet-class>
    </servlet>
    <servlet>
        <servlet-name>ServletLogout</servlet-name>
        <servlet-class>model.ServletLogout</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>ServletLogic</servlet-name>
        <url-pattern>/ServletLogic</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>ServletLogout</servlet-name>
        <url-pattern>/ServletLogout</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
</web-app>

index.jsp

<%@page import="java.util.Map"%>
<%@page import="java.util.HashMap"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <form action="ServletLogic" method="post">
            USER:<input type="text" name="user" id="user"/><br>
            PASS:<input type="password" name="pass" id="pass"/><br>
            <input type="submit" value="Login"/>
        </form>
    </body>
</html>

login.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <h1>My Page</h1>
        <form action="ServletLogout" method="post">
              <input type="submit" value="Logout"/>
        </form>
    </body>
</html>

ServletLogic.java //サーブレット

package model;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ServletLogic extends HttpServlet {

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();

        String user=request.getParameter("user");
        String pass=request.getParameter("pass");
        String res;
        if(user.equals("admin")&&pass.equals("admin"))
         res="Success!!!";
        else
          res="Failure!!!";
        request.getRequestDispatcher("login.jsp").forward(request, response); 

        try {

        } finally {            
            out.close();
        }
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }


    @Override
    public String getServletInfo() {
        return "Short description";
    }
}

ServletLogout.java // サーブレット

package model;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ServletLogout extends HttpServlet {

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {

        request.getRequestDispatcher("index.jsp").forward(request, response); 
        } finally {            
            out.close();
        }
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    @Override
    public String getServletInfo() {
        return "Short description";
    }
}
于 2013-03-04T13:41:53.807 に答える