Hi I did simple web application with servlet, to serve login and welcome page based on the session available. I had attached the code below,
whenever the user types the URL http://localhost:8888/
, he must take to the dashboard
servlet (as mention in the web.xml
)
In the dashboard servlet
, where session is checked
if the session is available, then I will serve welcome.html
page,
else I redirect to login servlet
In the login servlet
,
GET
method,
if the user have the session I redirect to the dashboard servlet
else I will the forward the welcome.html
page
after user entry the information, it will post the information to the login servlet
(i.e., POST)
POST
method,
I will get the username and password from the request, I will check value is admin
and password
if it is true create a session and redirect the user to the dashboard servlet
else I will redirect the user to the login servlet
(GET Method)
web application should need to have only these 2 URL,
(1) http://localhost:8888/
(2) http://localhost:8888/login
user should not aware of the type of page I am serving to them(html or jsp) but I need to serve HTML page only(this my requirement)
when I start the app engine, At first the request send to dashboard then it is redirected to the login servlet
but then its making loop for the dashboard once again,
I used System.out.println()
to track the control my output is
The admin console is running at http://localhost:8888/_ah/admin
Jul 25, 2017 11:12:55 AM com.google.appengine.tools.development.DevAppServerImpl doStart
INFO: Dev App Server is now running
-->dashboard servlet
get method
no session,
login servlet get method
request no session
-->dashboard servlet
get method
no session,
login servlet get method
request no session
-->dashboard servlet
get method
no session,
login servlet get method
request no session
-->dashboard servlet
get method
no session,
login servlet get method
request no session
-->dashboard servlet
get method
no session,
login servlet get method
request no session
-->dashboard servlet
get method
no session,
login servlet get method
request no session
-->dashboard servlet
get method
no session,
login servlet get method
request no session
-->dashboard servlet
get method
no session,
login servlet get method
request no session
-->dashboard servlet
get method
no session,
login servlet get method
request no session
-->dashboard servlet
get method
no session,
login servlet get method
request no session
-->dashboard servlet
get method
no session,
login servlet get method
request no session
-->dashboard servlet
get method
no session,
login servlet get method
request no session
-->dashboard servlet
get method
no session,
login servlet get method
request no session
-->dashboard servlet
get method
no session,
login servlet get method
request no session
-->dashboard servlet
get method
no session,
login servlet get method
request no session
-->dashboard servlet
get method
no session,
login servlet get method
request no session
-->dashboard servlet
get method
no session,
login servlet get method
request no session
-->dashboard servlet
get method
no session,
login servlet get method
request no session
-->dashboard servlet
get method
no session,
login servlet get method
request no session
-->dashboard servlet
get method
no session,
login servlet get method
request no session
-->dashboard servlet
get method
no session,
login servlet get method
request no session
-->dashboard servlet
get method
no session,
login servlet get method
request no session
-->dashboard servlet
get method
no session,
login servlet get method
request no session
-->dashboard servlet
get method
no session,
login servlet get method
request no session
-->dashboard servlet
get method
no session,
login servlet get method
request no session
-->dashboard servlet
get method
no session,
login servlet get method
request no session
-->dashboard servlet
get method
no session,
login servlet get method
request no session
-->dashboard servlet
get method
no session,
login servlet get method
request no session
-->dashboard servlet
get method
no session,
login servlet get method
request no session
-->dashboard servlet
get method
no session,
login servlet get method
request no session
-->dashboard servlet
get method
no session,
login servlet get method
request no session
-->dashboard servlet
get method
no session,
login servlet get method
request no session
-->dashboard servlet
get method
no session,
login servlet get method
request no session
-->dashboard servlet
get method
no session,
login servlet get method
request no session
-->dashboard servlet
get method
no session,
login servlet get method
request no session
-->dashboard servlet
get method
no session,
login servlet get method
request no session
-->dashboard servlet
get method
no session,
login servlet get method
request no session
-->dashboard servlet
get method
no session,
login servlet get method
request no session
-->dashboard servlet
get method
no session,
login servlet get method
request no session
-->dashboard servlet
get method
no session,
login servlet get method
request no session
-->dashboard servlet
get method
no session,
login servlet get method
request no session
-->dashboard servlet
get method
no session,
login servlet get method
request no session
-->dashboard servlet
get method
no session,
login servlet get method
request no session
-->dashboard servlet
get method
no session,
login servlet get method
request no session
-->dashboard servlet
get method
no session,
login servlet get method
request no session
-->dashboard servlet
get method
no session,
login servlet get method
request no session
-->dashboard servlet
get method
no session,
login servlet get method
request no session
-->dashboard servlet
get method
no session,
login servlet get method
request no session
-->dashboard servlet
get method
no session,
login servlet get method
request no session
-->dashboard servlet
get method
no session,
login servlet get method
request no session
-->dashboard servlet
get method
no session,
login servlet get method
request no session
-->dashboard servlet
get method
no session,
login servlet get method
request no session
-->dashboard servlet
get method
no session,
login servlet get method
request no session
-->dashboard servlet
get method
no session,
login servlet get method
request no session
-->dashboard servlet
get method
no session,
login servlet get method
request no session
-->dashboard servlet
get method
no session,
login servlet get method
request no session
-->dashboard servlet
get method
no session,
login servlet get method
request no session
-->dashboard servlet
get method
no session,
login servlet get method
request no session
-->dashboard servlet
get method
no session,
login servlet get method
request no session
-->dashboard servlet
get method
no session,
login servlet get method
request no session
-->dashboard servlet
get method
no session,
login servlet get method
request no session
-->dashboard servlet
get method
no session,
login servlet get method
request no session
-->dashboard servlet
get method
no session,
login servlet get method
request no session
-->dashboard servlet
get method
no session,
login servlet get method
request no session
-->dashboard servlet
get method
no session,
login servlet get method
request no session
-->dashboard servlet
get method
no session,
login servlet get method
request no session
-->dashboard servlet
get method
no session,
login servlet get method
request no session
-->dashboard servlet
get method
no session,
login servlet get method
request no session
-->dashboard servlet
get method
no session,
login servlet get method
request no session
-->dashboard servlet
get method
no session,
login servlet get method
request no session
-->dashboard servlet
get method
no session,
login servlet get method
request no session
-->dashboard servlet
get method
no session,
login servlet get method
request no session
-->dashboard servlet
get method
no session,
login servlet get method
request no session
-->dashboard servlet
get method
no session,
login servlet get method
request no session
-->dashboard servlet
get method
no session,
login servlet get method
request no session
-->dashboard servlet
get method
no session,
login servlet get method
request no session
-->dashboard servlet
get method
no session,
login servlet get method
request no session
-->dashboard servlet
get method
no session,
login servlet get method
request no session
-->dashboard servlet
get method
no session,
login servlet get method
request no session
-->dashboard servlet
get method
no session,
login servlet get method
request no session
-->dashboard servlet
get method
no session,
login servlet get method
request no session
-->dashboard servlet
get method
no session,
login servlet get method
request no session
-->dashboard servlet
get method
no session,
login servlet get method
request no session
-->dashboard servlet
get method
no session,
login servlet get method
request no session
-->dashboard servlet
get method
no session,
login servlet get method
request no session
-->dashboard servlet
get method
no session,
login servlet get method
request no session
-->dashboard servlet
get method
no session,
login servlet get method
request no session
-->dashboard servlet
get method
no session,
login servlet get method
request no session
-->dashboard servlet
get method
no session,
login servlet get method
request no session
-->dashboard servlet
get method
no session,
login servlet get method
request no session
-->dashboard servlet
get method
no session,
login servlet get method
request no session
-->dashboard servlet
get method
no session,
login servlet get method
request no session
-->dashboard servlet
get method
no session,
login servlet get method
request no session
-->dashboard servlet
get method
no session,
login servlet get method
request no session
-->dashboard servlet
get method
no session,
login servlet get method
request no session
-->dashboard servlet
get method
no session,
login servlet get method
request no session
-->dashboard servlet
get method
no session,
login servlet get method
request no session
-->dashboard servlet
get method
no session,
login servlet get method
request no session
-->dashboard servlet
get method
no session,
login servlet get method
request no session
-->dashboard servlet
get method
no session,
login servlet get method
request no session
-->dashboard servlet
get method
no session,
login servlet get method
request no session
-->dashboard servlet
get method
no session,
login servlet get method
request no session
-->dashboard servlet
get method
no session,
login servlet get method
request no session
-->dashboard servlet
get method
no session,
login servlet get method
request no session
-->dashboard servlet
get method
no session,
QUESTION:
How can solve this problem?
If I change url pattern in web.xml
as <url-pattern>/index</url-pattern>
and in the servlet whereever redirection to the /
I changed to /index
for the dashboard servlet
it is working fine, but how can I make use of the "/" for servlet
I have attached the source code below,
dashboard servlet
package servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
@SuppressWarnings("serial")
public class DashboardServlet extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("-->dashboard servlet");
System.out.println("get method");
HttpSession session = req.getSession(false);
if (session != null) {
System.out.println("session is available");
req.getRequestDispatcher("/WEB-INF/welcome.html").forward(req, resp);
} else {
System.out.println("no session,");
resp.sendRedirect("/login");
}
}
}
Login Servlet
package servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
@SuppressWarnings("serial")
public class LoginServlet extends HttpServlet {
public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// TODO Auto-generated method stub
System.out.println("-->>>login servlet post method");
String name = req.getParameter("username");
String password = req.getParameter("password");
if (name.equals("admin") && password.equals("password")) {
HttpSession session = req.getSession();
session.setAttribute("sessionUserName", name);
resp.sendRedirect("/");
} else {
resp.sendRedirect("/login");
}
}
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("login servlet get method");
HttpSession session = req.getSession(false);
if (session != null) {
System.out.println("request with session");
resp.sendRedirect("/");
} else {
System.out.println("request no session");
req.getRequestDispatcher("/WEB-INF/login.html").forward(req, resp);
}
}
}
web.xml
<web-app>
<servlet>
<servlet-name>Login</servlet-name>
<servlet-class>servlet.LoginServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Login</servlet-name>
<url-pattern>/login</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>Dashboard</servlet-name>
<servlet-class>servlet.DashboardServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Dashboard</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
appengine-web.xml
<?xml version="1.0" encoding="utf-8"?>
<appengine-web-app xmlns="http://appengine.google.com/ns/1.0">
<application></application>
<version>1</version>
<threadsafe>true</threadsafe>
<system-properties>
<property name="java.util.logging.config.file" value="WEB-INF/logging.properties" />
</system-properties>
<sessions-enabled>true</sessions-enabled>
</appengine-web-app>
login.html
<!DOCTYPE html>
<html>
<head>
<title>Insert title here</title>
</head>
<body>
<form action="/login" method="post">
User name :<input type='text' name='username'>Password :<input
type='password' name='password'><input type='submit'
value='Submit'>
</form>
</body>
</html>
welcome.html
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>welcome here
</body>
</html>
Thank you,
You should not check only whether session is null. You should put a value like 'logged_in' into session as attribute. Then, control it for deciding where to route.
Update:
As a quick solution, route doGet inside LoginServlet to doPost and change doPost as below: