Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Servlets and the fly likes how to run a servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "how to run a servlet" Watch "how to run a servlet" New topic
Author

how to run a servlet

sandya patel
Greenhorn

Joined: Apr 23, 2005
Posts: 21
HI all
I am new to java world. i have assignments to work with servlet. I created .java file, in sample_webapp -->WEB-INF --->classes and test.html. test.jsp in sample_webapp. i complied java file and set permissions to all files. when i am deploying in tomcat, servlet is not running. it says false. i dont know where the error is. i think i am giving wrong context path. kindly throw me ideas.
waht should be given in a context path?? and how can i run servlet??
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Post your servlet code.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
sandya patel
Greenhorn

Joined: Apr 23, 2005
Posts: 21
Here is the code i used:
------------------------------------------------------------------------
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class HelloWorld extends HttpServlet
{
public void doGet (HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
String method = request.getMethod();
String connection = request.getHeader("Connection");
String accept = request.getHeader("Accept");
String accept_encoding = request.getHeader("Accept-Encoding");

String host = request.getHeader("Host");
String referer = request.getHeader("Referer");
String user_agent = request.getHeader("User-Agent");
PrintWriter out;
// set content type and other response header fields first

response.setContentType("text/html");
// then write the data of the response
out = response.getWriter();
out.println("<html>");
out.println("<head>");
out.println(" <title>Header results</title>");
out.println("</head>");
out.println("<body bgcolor=\"#FFFFFF\">");
out.println("<table width=\"75%\" border=\"1\">");
out.println("<tr>");
out.println(" <td width=\"51%\">method</td>");
out.println(" <td width=\"49%\">" + method + "</td>");
out.println(" </tr>");
out.println(" <tr>");
out.println(" <td width=\"51%\">connection</td>");
out.println(" <td width=\"49%\">"+ connection + "</td>");
out.println(" </tr>");
out.println(" <tr>");
out.println(" <td width=\"51%\">accept</td>");
out.println(" <td width=\"49%\">"+ accept + "</td>");
out.println(" </tr>");
out.println(" <tr>");
out.println(" <td width=\"51%\">Accept Encoding</td>");
out.println(" <td width=\"49%\">"+ accept_encoding + "</td>");
out.println(" </tr>");
/* out.println(" <tr>");
out.println(" <td width=\"51%\">cookies</td>");
out.println(" <td width=\"49%\"> + "</td>");
out.println(" </tr>");
for(int i = 0; i< Cookies.length;i++)
{
out.println(" <tr>");
out.println(" <td width=\"51%\"></td>");
out.println(" <td width=\"49%\">"+ Cookies[i] + "</td>");
out.println(" </tr>");
} */
out.println(" <tr>");
out.println(" <td width=\"51%\">Host</td>");
out.println(" <td width=\"49%\">"+ host + "</td>");
out.println(" </tr>");
out.println(" <tr>");
out.println(" <td width=\"51%\">Referer</td>");
out.println(" <td width=\"49%\">" + referer + "</td>");
out.println(" </tr>");
out.println(" <tr>");
out.println(" <td width=\"51%\">User Agent</td>");
out.println(" <td width=\"49%\">" + user_agent + "</td>");
out.println(" </tr>");
out.println("</table>");
out.println("</body>");
out.println("</html>");
out.close();
}
}
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Did you create an entry and a servlet-mapping for it in your web.xml file?
What URL are you using when trying to access it from a browser?

Also, have you verified that Tomcat is running?
Were you able to see the Tomcat welcome page when you hit:
http://localhost:8080/
?
Shailesh Chandra
Ranch Hand

Joined: Aug 13, 2004
Posts: 1081

[Posted at same time with ben's reply]

can you paste content of web.xml as well,because your servlet code is ok.
also add the url you are typing to call the servlet

Shailesh
[ April 23, 2005: Message edited by: Shailesh Chandra ]

Gravitation cannot be held responsible for people falling in love ~ Albert Einstein
sandya patel
Greenhorn

Joined: Apr 23, 2005
Posts: 21
Hi
i did mapping in web.xml file. but its not running in tomcat.
thats the problem
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Post your web.xml
sandya patel
Greenhorn

Joined: Apr 23, 2005
Posts: 21
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE web-app (View Source for full doctype...)>
- <web-app>
<display-name>A sample webapp</display-name>
<description></description>
<distributable/>
<context-param>
<param-name></param-name>
<param-value></param-value>
<description></description>
</context-param>
- <servlet>
<servlet-name>Hello</servlet-name>
<servlet-class>Hello</servlet-class>
- <!--
<init-param>
<param-name></param-name>
<param-value></param-value>
<description></description>
</init-param>


-->
- <!--
<load-on-startup>3</load-on-startup>



-->
- <!--
<load-on-startup></load-on-startup>


-->
</servlet>
- <servlet-mapping>
<servlet-name>Hello</servlet-name>
<url-pattern>/Hello</url-pattern>
</servlet-mapping>
- <!--
<session-config>
<session-timeout>30</session-timeout>
</session-config>


-->
- <!--

<welcome-file-list>
<welcome-file> </welcome-file>
<welcome-file> </welcome-file>
</welcome-file-list>


-->
</web-app>
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

You're servlet class is named HelloWorld but you are referring to "Hello" in your web.xml. You're java classes should also be in packages but I don't think that's what's stopping it from working.

Also, why do you have all of those empty init-param tags in there?


If you want a very simple example that you can run and use as a reference, download SimpleServlet.war from http://simple.souther.us.
It's a war file so all you have to do is drop it in your TOMCAT_HOME/webapps directory and Tomcat will unpack it and deploy it.

Once you do that, you'll have a working example of a web.xml file that you can start with.
Neeraj Dheer
Ranch Hand

Joined: Mar 30, 2005
Posts: 225
Ben is right. the entry in the web.xml seems to point to a class file that does not exist.

it should be


and the url to refer to this will be

http://localhost:8080/sample_webapp/Hello

or similar. note the /sample_webapp/Hello
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: how to run a servlet
 
Similar Threads
FileNotFoundException: no resource '/servlet/ (JBuilder7 and Weblogic7)
Servlet package and Tomcat Question
Deploying Servlet in Tomcat 5.0
context path in java class
Tomcat Restarting