Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

problem in excuting Basicservlet

 
chinna kumar
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
am trying to access servlet thru browser using using url: http://localhost:8080/venu/servlet/BasicServlet , it says not servlet not available.

this is my code

directory struture is....webapps/venu/WEB-INF/classess, under classes i have placed this servlet.

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
public class BasicServlet extends HttpServlet
{
public void doGet(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException
{
doPost(req,res);
}
public void doPost(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException
{
ServletContext context=getServletContext();
String username=(String)context.getAttribute("username");
if(username==null)
{
// username=new String("kumar");
context.setAttribute("username",new String("kumar"));
}
res.setContentType("text/html");
PrintWriter out=res.getWriter();
out.println("<html>");
out.println("<head><title>BasicServlet</title></head>");
out.println("<body>");
out.println("The Current UserName Is:::::"+username);
}
public static void main(String[] args)
{
System.out.println("???/");
}
}



please help me out...


Regards
Chinna
 
Praful Thakare
Ranch Hand
Posts: 642
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
try doing the following

----------------------
open tomcat\conf\web.xml
uncomment following entry
restart tomcat
invoke the servlet again


<!-- The mapping for the invoker servlet -->
<!--
<servlet-mapping>
<servlet-name>invoker</servlet-name>
<url-pattern>/servlet/*</url-pattern>
</servlet-mapping>
-->
 
chinna kumar
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Praful Thakare,
it works!!!

Thanx A Lot

Chinna
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
He's quite right, but just to clarify:

The correct way to execute a Servlet is to map it to a URL and then access it through that URL eg /venu/MyServlet
Accessing servlets as /venu/servlet/ClassName is a horrible old thing that they are trying to get people away from. By default it is disabled, but following the instructions above you can enable the invoker Servlet in Tomcat which allows you to access Servlets in this way.

The invoker Servlet is a special Servlet mapped to /venu/servlet/*, looks at what you asked to invoke and calls it. The big problem is that if you forget to turn it off it allows direct access to all servlets as long as the client knows the servlet class name. This can be a security risk or cause instable applications when used in production, but is OK when learning, as long as you learn how to map Servlets soon!

Dave
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To put it mildly, un-commenting the invoker servlet is starting off on the wrong foot:
http://faq.javaranch.com/view?InvokerServlet
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic