This is my Java class
package login.example;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.persistence.Id;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
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("/UserOne")
public class UserOne extends HttpServlet {
private static final long serialVersionUID = -7250065889869767422L;
@Id
private long id;
private String name;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void setId(long id) {
this.id = id;
}
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
PrintWriter out = response.getWriter();
response.setContentType("text/html");
long id = Long.parseLong(request.getParameter("id"));
String name = request.getParameter("name");
// String userRePass = request.getParameter("userRePassWord");
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/any", "root", "jaygurudev1@");
PreparedStatement ps = con.prepareStatement("Select id,name from user where id=? and name=?");
ps.setLong(1, id);
ps.setString(2, name);
ResultSet rs = ps.executeQuery();
// HttpServletRequest request = null;
if (rs.next()) {
HttpSession session = request.getSession(true);
session.setAttribute("id", id);
session.setAttribute("name", name);
ServletContext context = request.getServletContext();
response.setHeader("Cache-control", "no-cache"); // HTTP 1.1
response.setHeader("Cache-control", "no-store");
response.setHeader("Pragma", "no-cache"); // HTTP 1.0
response.setDateHeader("Expire", 0);
RequestDispatcher dispatcher = context.getRequestDispatcher("/admin.jsp");
dispatcher.forward(request, response);
// or you can write whole thing in one line as ........
// getServletContext().getRequestDispatcher("/success").forward(request,
// response);
} else {
// request.setAttribute("wrongUser", userName);
ServletContext context = request.getServletContext();
response.setHeader("Cache-control", "no-cache"); // HTTP 1.1
response.setHeader("Cache-control", "no-store");
response.setHeader("Pragma", "no-cache"); // HTTP 1.0
response.setDateHeader("Expire", 0);
RequestDispatcher dispatcher = context.getRequestDispatcher("/login.jsp");
dispatcher.forward(request, response);
// or you can write whole thing in one line as ........
// getServletContext().getRequestDispatcher("/fail").forward(request,
// response);
}
} catch (SQLException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
This is my login.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<div style="text-align: center">
<h1>admin login</h1>
<%
response.setHeader("Cache-Control", "no-cache, no-store, must-revalidate"); // HTTP 1.1
response.setHeader("Pragma", "no-cache"); // HTTP 1.0
response.setDateHeader("Expires", 0); // Proxies.
%>
<form action="/UserOne" method="get">
<label for="id">Id</label> <input name="id" /> <label for="name">Name</label>
<input type="text" name="name" />
<button type="submit">Login</button>
</form>
</div>
</body>
</html>
This is my admin.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.Statement"%>
<%@page import="java.sql.Connection"%>
<%@page import="java.io.IOException"%>
<%@page import="java.io.PrintWriter"%>
<!DOCTYPE html>
<html>
<head>
<style>
body {
background-image: url(/images/admin.jpg);
background-repeat: repeat;
background-size: 640px 640px;
}
</style>
</head>
<body>
<%
response.setHeader("Cache-control", "no-cache"); // HTTP 1.1
response.setHeader("Cache-control", "no-store");
response.setHeader("Pragma", "no-cache"); // HTTP 1.0
response.setDateHeader("Expire", 0);
%>
<a href="/userone">get all users</a>
<h3>view user</h3>
getMobileInfo
<a href="/indexone">get all mobile details</a>
<form action="/LogoutServlet" method="get">
<input type="submit" value="logout">
</form>
</body>
</html>
This is logout servlet
package login.example;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
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(name = "LogoutServlet", urlPatterns = { "/LogoutServlet" })
public class LogoutServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public LogoutServlet() {
}
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
PrintWriter out = response.getWriter();
response.setContentType("text/html");
HttpSession session = request.getSession();
session.removeAttribute("id");
session.removeAttribute("name");
request.getSession(false);
session.invalidate();
out.println("Thank you,you are successfully logged out");
// response.setHeader("Cache-Control", "no-cache,no-store,must-revalidate");
HttpServletResponse httpResponse = (HttpServletResponse) response;
httpResponse.setHeader("Cache-Control", "no-cache, no-store, must-revalidate"); // HTTP 1.1
httpResponse.setHeader("Pragma", "no-cache"); // HTTP 1.0
httpResponse.setDateHeader("Expires", 0); // Proxies.
// request.getRequestDispatcher("login.jsp").include(request, response);
response.sendRedirect("login.jsp");
}
}
What should I add or remove in order to get desired output such as it does not show previous page after I logged out? What is wrong with my code? Should I need any dependency or anything else to achieve this? How to solve this?
my url is like http://localhost:8080/home.html
then http://localhost:8080/login.jsp?
and http://localhost:8080/UserOne?id=1&name=ritu
CodePudding user response:
When you click back button the the url(/UserOne?id=1&name=ritu) submits the old id and name making it to login to the page again. Send the login credentials in post paramater to fix the issue.
ie: In login.jsp the submit method should be post