Two Laptop Bag*
The moose likes Servlets and the fly likes Pls help : I get error when i try to access servlet directly Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Pls help : I get error when i try to access servlet directly" Watch "Pls help : I get error when i try to access servlet directly" New topic
Author

Pls help : I get error when i try to access servlet directly

Subbu Aswathanarayan
Ranch Hand

Joined: Jun 22, 2001
Posts: 73
Hi Everyone,
I am writing a small web based application which requires user authentication.For this I am using two servlets and a html form.The user enters the loginid and password in the html form and this information is passed to the first servlet.the servlet verifies the login and if its ok sets a session id and invokes another servlet using the requestDispatcher.forward() method.The second servlet retrives the session id and if its a valid session, processes the user request.
To ensure that no one gets to the second servlet directly(by bypassing the login page and the first servlet), I try to retrieve the session id in the 2nd servlet and if there is no such id i print a error message.My problem is when i try to access the second servlet directly, "i get a page not found" error rather than my own error message.
i am attaching the code for my second servlet.any help in this regard would be greatly appreciated.
System info
Windows NT 4.0
IIS 4.0
JRun 3.1 servlet engine
Subbu

import java.io.*;
import java.sql.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class loggedin extends HttpServlet
{
public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException
{
res.setContentType("text/html");
PrintWriter out = res.getWriter();
out.println("<html><head><title>My First Servlet");
out.println("</title></head><body>");
ServletContext context = getServletContext();
String attributecookie = (String)context.getAttribute("XXXXXXXXX");
HttpSession session = req.getSession(false);
String sessioncookie = (String)session.getValue("XXXXXXXXX");
out.println("<p>The Session id is" + sessioncookie + "<p>");
out.println("<p>The attribute id is" + attributecookie + "<p>");
if(((attributecookie.length())==0) | | ((sessioncookie.length())==0))
{
out.println("<p>You are not authorized to view this page<p>");
out.println("<p>Please relogin<p>");
out.println("</body></html>");
}
else
{
if((attributecookie.compareTo(sessioncookie))==0)
{
out.println("<p>The Session id is" + sessioncookie + "<p>");
out.println("<p>Session established<p>");
out.println("</body></html>");
try
{
session.removeValue("XXXXXXXXX");
}
catch(IllegalStateException e)
{
out.println("Illegal State exception");
}
session.invalidate();
//context.removeAttribute("XXXXXXXXX");
}
else
{
out.println("<p>You are not authorized to view this page<p>");
out.println("<p>Please relogin<p>");
out.println("</body></html>");
}
}
}
}

Subbu
david, chen
Greenhorn

Joined: Jan 18, 2001
Posts: 15
Hi, there:
The problem probably happens on your URL used in your 1st servlet when you tried to forward request. The URL used in getRequestDispatcher(url) is supposed to be one relative to the server root.
David
Subbu Aswathanarayan
Ranch Hand

Joined: Jun 22, 2001
Posts: 73
What I am trying to say is, when i try to access directly without going thro the first servlet, i get a "page not found" error message.i want to print my own error message and probably redirect the user to the login page again.but i am not able to do so.

Originally posted by Subbu Aswathanarayan:
Hi Everyone,
I am writing a small web based application which requires user authentication.For this I am using two servlets and a html form.The user enters the loginid and password in the html form and this information is passed to the first servlet.the servlet verifies the login and if its ok sets a session id and invokes another servlet using the requestDispatcher.forward() method.The second servlet retrives the session id and if its a valid session, processes the user request.
To ensure that no one gets to the second servlet directly(by bypassing the login page and the first servlet), I try to retrieve the session id in the 2nd servlet and if there is no such id i print a error message.My problem is when i try to access the second servlet directly, "i get a page not found" error rather than my own error message.
i am attaching the code for my second servlet.any help in this regard would be greatly appreciated.
System info
Windows NT 4.0
IIS 4.0
JRun 3.1 servlet engine
Subbu

import java.io.*;
import java.sql.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class loggedin extends HttpServlet
{
public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException
{
res.setContentType("text/html");
PrintWriter out = res.getWriter();
out.println("<html><head><title>My First Servlet");
out.println("</title></head><body>");
ServletContext context = getServletContext();
String attributecookie = (String)context.getAttribute("XXXXXXXXX");
HttpSession session = req.getSession(false);
String sessioncookie = (String)session.getValue("XXXXXXXXX");
out.println("<p>The Session id is" + sessioncookie + "<p>");
out.println("<p>The attribute id is" + attributecookie + "<p>");
if(((attributecookie.length())==0) | | ((sessioncookie.length())==0))
{
out.println("<p>You are not authorized to view this page<p>");
out.println("<p>Please relogin<p>");
out.println("</body></html>");
}
else
{
if((attributecookie.compareTo(sessioncookie))==0)
{
out.println("<p>The Session id is" + sessioncookie + "<p>");
out.println("<p>Session established<p>");
out.println("</body></html>");
try
{
session.removeValue("XXXXXXXXX");
}
catch(IllegalStateException e)
{
out.println("Illegal State exception");
}
session.invalidate();
//context.removeAttribute("XXXXXXXXX");
}
else
{
out.println("<p>You are not authorized to view this page<p>");
out.println("<p>Please relogin<p>");
out.println("</body></html>");
}
}
}
}

Alag Solai
Greenhorn

Joined: Jun 26, 2001
Posts: 3
Hi,
This has nothing to do with your servlet code. As, you are getting page not found error, Check for Servlet mapping in the server and if you specify URL correctly.
Cynthia Yao
Ranch Hand

Joined: Nov 06, 2000
Posts: 92
If I am not mistaken, your servlet do not have doGet() method. that may be the problem
Subbu Aswathanarayan
Ranch Hand

Joined: Jun 22, 2001
Posts: 73
Hi Cynthia,
Its working fine after i put the doGet() method.thanks a lot.
Subbu
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Pls help : I get error when i try to access servlet directly