これを試してください...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";
}
}