File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes problem in running a servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "problem in running a servlet" Watch "problem in running a servlet" New topic
Author

problem in running a servlet

subhala subramanian
Greenhorn

Joined: Jan 31, 2010
Posts: 10
http://localhost:8080/servlet/HelloWorldExample
sagar powar
Greenhorn

Joined: Jan 13, 2010
Posts: 8
Please describe your question. What is problem, stack trace (if any).


SCJP 1.4 - 93%
subhala subramanian
Greenhorn

Joined: Jan 31, 2010
Posts: 10
while running a servlet im getting an error HTTP 404 resource not available

development directory

webapps
sample
hello.jsp
WEB-INF
web.xml
classes
HelloWorldExample.java

HelloWorldExample.java

<code>

import java.io.*;
import java.text.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;



public class HelloWorldExample extends HttpServlet {


public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException
{
ResourceBundle rb =
ResourceBundle.getBundle("LocalStrings",request.getLocale());
response.setContentType("text/html");
PrintWriter out = response.getWriter();

out.println("<html>");
out.println("<head>");

String title = rb.getString("helloworld.title");

out.println("<title>" + title + "</title>");
out.println("</head>");
out.println("<body bgcolor=\"white\">");
out.println("<a href=\"../helloworld.html\">");
out.println("<img src=\"../images/code.gif\" height=24 " +
"width=24 align=right border=0 alt=\"view code\"></a>");
out.println("<a href=\"../index.html\">");
out.println("<img src=\"../images/return.gif\" height=24 " +
"width=24 align=right border=0 alt=\"return\"></a>");
out.println("<h1>" + title + "</h1>");
out.println("</body>");
out.println("</html>");
}
}

</code>

web.xml


<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">

<web-app>
<servlet>
<servlet-name>HelloWorldExample</servlet-name>
<servlet-class>HelloWorldExample</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HelloWorldExample</servlet-name>
<url-pattern>/servlet/HelloWorldExample</url-pattern>
</servlet-mapping>
</web-app>

my classpath is set CLASSPATH=,;C:/Program Files/Apache Software Foundation/Tomcat 5.5/common/lib'servlet-api.jar
sagar powar
Greenhorn

Joined: Jan 13, 2010
Posts: 8
From directory structure you given it is not clear where you have placed web.xml file and HelloWorldExample.class file.

Web application directory structure in server should be as follows:



Place web.xml file inside WEB-INF directory.
WEB-INF directory contains :
  • WEB-INF/web.xml deployment descriptor
  • WEB-INF/classes directory
  • WEB-INF/lib directory


  • For more information on structure of web applications follow link - http://www.jsptube.com/servlet-tutorials/web-application-directory-structure.html

    To access the servlet use url as - http://localhost:8080/sample/servlet/HelloWorldExample
    (you missed context root (sample) in URL)
    Ravi Kiran Va
    Ranch Hand

    Joined: Apr 18, 2009
    Posts: 2234

    seems that you are a beginner to J2EE .

    The best thing is suggest you is that if you have Tomcat installed , under webapps take any directory , chnage that to your context name ,put ypur files and configure inside web.xml for servlets .

    Thats it .


    Save India From Corruption - Anna Hazare.
    subhala subramanian
    Greenhorn

    Joined: Jan 31, 2010
    Posts: 10
    thank you sir..............we got it

    we tried the same for our project online bookstore

    development directory

    ->bookstore
    |-home.jsp
    |-login.jsp
    |-cart.jsp
    |-register.jsp
    |-order.jsp
    |-index.jsp
    |-category.jsp
    |-checkout.jsp

    ->WEB-INF
    ->classes
    |-login.class
    |-logout.class
    |-book.class
    |-bookdata.class
    |-customer.class
    |-category.class
    |-checkout.class
    |-register.class
    |-order.class
    |-database.class
    -> lib
    |-bookstore.jar
    |- mysql-connector.jar

    when we compiled the jsp files we got

    exception

    org.apache.jasper.JasperException
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:370)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:802)


    root cause

    java.lang.NullPointerException
    org.apache.jsp.cart_jsp._jspService(org.apache.jsp.cart_jsp:162)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:97)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:322)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:802)


    sagar powar
    Greenhorn

    Joined: Jan 13, 2010
    Posts: 8
    Please mention on which activity you got an error (any URL) and paste exact stack trace from log file. This might help us to recognize the problem.
    subhala subramanian
    Greenhorn

    Joined: Jan 31, 2010
    Posts: 10
    while compiling home.jsp ......getting an exception error

    http://localhost:8080/bookstoreNew/home.jsp

    Stacktrace:
    org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:451)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:373)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:329)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:265)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:729)


    root cause

    java.lang.NullPointerException
    org.apache.jsp.home_jsp._jspService(home_jsp.java:138)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:98)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:331)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:329)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:265)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:729)

    sagar powar
    Greenhorn

    Joined: Jan 13, 2010
    Posts: 8
    Some code in "home.jsp" file is causing error. Please post contents of home.jsp file.
    subhala subramanian
    Greenhorn

    Joined: Jan 31, 2010
    Posts: 10
    <%@ page contentType="text/html; charset=iso-8859-1" language="java" import="java.sql.*" errorPage="" %>
    <%@ page import="bookstore.*" %>
    <%@ page import="java.util.*" %>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>The Book Store</title>
    <style type="text/css">
    <!--
    body {
    margin-bottom:0px;
    margin-top:0px;
    background:url(images/bg1.gif) repeat-x;
    background-color:#e7e7e7;
    FONT-FAMILY: Verdana,sans-serif;
    FONT-SIZE: 12px; COLOR: #000;
    }

    A { text-decoration:none; color:#990000; }
    A:hover {color:#000; text-decoration:underline}
    A:visited {text-decoration:none;}

    .style6 {color: #fff; font-family: "Comic Sans MS"; font-size: 36px; }
    .style10{padding-left:10px; padding-right:10px;}
    .style10 H3 {
    PADDING-RIGHT: 0px; PADDING-LEFT: 10px; FONT-WEIGHT: normal; PADDING-BOTTOM: 4px; MARGIN: 1em 0px 0px; TEXT-TRANSFORM: uppercase; PADDING-TOP: 4px; BORDER-BOTTOM: #999 1px solid
    }
    .style11{BORDER-RIGHT: #999 1px solid}
    .style12{padding-left:25px; BORDER-BOTTOM: #999 1px solid;PADDING-TOP: 4px;PADDING-BOTTOM: 4px;TEXT-TRANSFORM: uppercase;}
    .style13{padding-left:10px; padding-right:10px;}
    .style13 H3 {
    PADDING-RIGHT: 0px; PADDING-LEFT: 10px; FONT-WEIGHT: normal; PADDING-BOTTOM: 4px; MARGIN: 1em 0px 0px; TEXT-TRANSFORM: uppercase; PADDING-TOP: 4px; BORDER-BOTTOM: #999 1px solid
    }
    .style14 {
    font-size: 14px;
    font-weight: bold;
    }
    .style15 {
    PADDING-RIGHT: 10px; PADDING-LEFT: 10px; PADDING-BOTTOM: 5px; PADDING-TOP: 5px; BORDER-BOTTOM: #999 1px solid

    }
    .style16 {
    color: #0033CC;
    font-weight: bold;
    }
    -->
    </style>
    </head>




    <body>
    <table width="100%" border="0" cellspacing="0" cellpadding="0" height="550">
    <tr>
    <td height="50" colspan="3"><table width="100%" border="0" cellspacing="0" cellpadding="0" height="70">
    <tr>
    <td width="50%" > <span class="style6"> The Book Store </span></td>
    <td width="50%" align="right" valign="bottom"><% if(session.getAttribute("customer") != null) {

    Customer customer = (Customer) session.getAttribute("customer"); %>

    <p>Hello <%= customer.getFirstName() %>, <a href="Signout">Sign Out</a> </p>
    <p><a href="ManageCart?c=v">View Cart </a>, </p>

    <% } else { %> <p>Hello Guest, <a href="login.jsp">Sign In</a> </p>
    <p><a href="ManageCart?c=v">View Cart </a> </p> <% } %></td>
    </tr>
    </table></td>
    </tr>
    <tr>
    <td height="5" colspan="3" valign="top" ><hr/></td>
    </tr>

    <tr>
    <td width="17%" height="450" valign="top" class="style10">
    <h3> categories</h3>

    <table width="100%" border="0" cellpadding="0" cellspacing="0" >

    <%
    ArrayList categories = (ArrayList) session.getAttribute("categories");
    for (Iterator iter = categories.iterator(); iter.hasNext();) {

    Category category = (Category) iter.next();

    %>
    <tr>
    <td class="style12"><a href="Category?c=<%=category.getCategoryId() %>"><%= category.getCategoryName() %></a> </td>
    </tr>

    <% } %>




    </table></td>
    <td width="3%" valign="top"></td>
    <td width="80%" valign="top" class="style13">
    <h3> New Releases</h3>

    <%
    ArrayList newBooks = (ArrayList) session.getAttribute("newBooks");
    for (Iterator iter = newBooks.iterator(); iter.hasNext();) {

    Book book = (Book) iter.next();

    %>




    <table width="100%" height="117" border="0" cellpadding="0" cellspacing="0" class="style15">
    <tr>
    <td width="19%" rowspan="2" valign="top"><div align="center"><a href="Detail?b=<%=book.getIsbn() %>"><img src="<%=book.getThumbnail() %>" width="85" height="98" border="0"/></a></div> </td>
    <td width="81%" height="45"><p><span class="style14"><a href="Detail?b=<%=book.getIsbn() %>"><%= book.getTitle() %></a></span><br />
    By <%= book.getAuthor() %><br/>
    Price:<span class="style16">Rs.<%= book.getPrice() %>
    </span></p></td>
    </tr>
    <tr>
    <td height="52" valign="top"><%= book.getDescription() %>… <a href="Detail?b=<%=book.getIsbn() %>">Read more.</a></td>
    </tr>
    </table>


    <% } %>






    </td>
    </tr>
    <tr>
    <td height="5" colspan="3" valign="top" ><hr/></td>
    </tr>
    <tr>
    <td height="40" colspan="3" valign="top" ><div align="center">The Book Store </div></td>
    </tr>
    </table>
    </body>
    </html>

    Bear Bibeault
    Author and ninkuma
    Marshal

    Joined: Jan 10, 2002
    Posts: 61610
        
      67

    Please be sure to use code tags when posting code to the forums. Unformatted code is extremely hard to read and many people that might be able to help you will just move along to posts that are easier to read. Please read this for more information.

    You can go back and change your post to add code tags by clicking the button on your post.

    [Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
    subhala subramanian
    Greenhorn

    Joined: Jan 31, 2010
    Posts: 10
    <code>
    <%@ page contentType="text/html; charset=iso-8859-1" language="java" import="java.sql.*" errorPage="" %>
    <%@ page import="bookstore.*" %>
    <%@ page import="java.util.*" %>

    <jsp:useBean id="signinForm" class="bookstore.SigninForm" scope="request">
    <jsp:setProperty name="signinForm" property="*"/>
    </jsp:useBean>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>The Book Store</title>
    <style type="text/css">
    <!--
    body {
    margin-bottom:0px;
    margin-top:0px;
    background:url(images/bg1.gif) repeat-x;
    background-color:#e7e7e7;
    FONT-FAMILY: Verdana,sans-serif;
    FONT-SIZE: 12px; COLOR: #000;
    }

    A { text-decoration:none; color:#990000; }
    A:hover {color:#000; text-decoration:underline}
    A:visited {text-decoration:none;}

    .style6 {color: #fff; font-family: "Comic Sans MS"; font-size: 36px; }
    .style10{padding-left:10px; padding-right:10px;}
    .style10 H3 {
    PADDING-RIGHT: 0px; PADDING-LEFT: 10px; FONT-WEIGHT: normal; PADDING-BOTTOM: 4px; MARGIN: 1em 0px 0px; TEXT-TRANSFORM: uppercase; PADDING-TOP: 4px; BORDER-BOTTOM: #999 1px solid
    }
    .style11{BORDER-RIGHT: #999 1px solid}
    .style12{padding-left:25px; BORDER-BOTTOM: #999 1px solid;PADDING-TOP: 4px;PADDING-BOTTOM: 4px;TEXT-TRANSFORM: uppercase;}
    .style13{padding-left:10px; padding-right:10px;}
    .style13 H3 {
    PADDING-RIGHT: 0px; PADDING-LEFT: 10px; FONT-WEIGHT: normal; PADDING-BOTTOM: 4px; MARGIN: 1em 0px 0px; TEXT-TRANSFORM: uppercase; PADDING-TOP: 4px; BORDER-BOTTOM: #999 1px solid
    }
    .style14 {
    font-size: 14px;
    font-weight: bold;
    }
    .style15 {
    PADDING-RIGHT: 10px; PADDING-LEFT: 10px; PADDING-BOTTOM: 5px; PADDING-TOP: 5px; BORDER-BOTTOM: #999 1px solid

    }

    .style17 {
    PADDING-RIGHT: 10px; PADDING-LEFT: 10px; PADDING-BOTTOM: 5px; PADDING-TOP: 5px; BORDER-BOTTOM: #999 1px solid; border-left:#999 1px solid; border-right:#999 1px solid; border-top:#999 1px solid;

    }
    .style16 {
    color: #0033CC;
    font-weight: bold;
    }
    .style18 {
    color: #000;
    font-weight: bold;
    text-align:center;
    BORDER-BOTTOM: #999 1px solid;
    }
    .style19 {


    text-align:center;

    }
    -->
    </style>
    </head>




    <body>
    <table width="100%" border="0" cellspacing="0" cellpadding="0" height="550">
    <tr>
    <td height="50" colspan="3"><table width="100%" border="0" cellspacing="0" cellpadding="0" height="70">
    <tr>
    <td width="50%" > <span class="style6"> The Book Store </span></td>
    <td width="50%" align="right" valign="bottom">
    <p><a href="ManageCart?c=v">View Cart </a> </p></td>
    </tr>
    </table></td>
    </tr>
    <tr>
    <td height="5" colspan="3" valign="top" ><hr/></td>
    </tr>

    <tr>
    <td width="17%" height="450" valign="top" class="style10">
    <h3> categories</h3>

    <table width="100%" border="0" cellpadding="0" cellspacing="0" >

    <%
    ArrayList categories = (ArrayList) session.getAttribute("categories");
    for (Iterator iter = categories.iterator(); iter.hasNext();) {

    Category category = (Category) iter.next();

    %>

    <tr>
    <td class="style12"><a href="Category?c=<%=category.getCategoryId() %>"><%= category.getCategoryName() %></a> </td>
    </tr>

    <% } %>




    </table></td>
    <td width="3%" valign="top"></td>
    <td width="80%" valign="top" class="style13">
    <h3> Customer Login </h3><br />




    <form action="Signin" method="post">
    <table width="50%" border="0" cellspacing="0" cellpadding="0" align="center" class="style17">
    <tr>
    <td colspan="2" align="center" height="20"></td>
    </tr>
    <tr>
    <td colspan="2" align="center"><font color=red><%=signinForm.getErrorMsg("error")%></font></td>
    </tr>
    <tr>
    <td width="46%"><div align="right">Username</div></td>
    <td width="54%"><input type="text" name="userName" value="<%=signinForm.getUserName() %>"/>
    <br><font color=red><%=signinForm.getErrorMsg("userName")%></font></td>
    </tr>
    <tr>
    <td><div align="right">Password</div></td>
    <td><input type="password" name="password" value="<%=signinForm.getPassword()%>" />
    <br><font color=red><%=signinForm.getErrorMsg("password")%></font></td>
    </tr>

    <tr>
    <td></td>
    <td>
    <input type="submit" name="Submit" value="Login" style="width:60%" />
    </td>
    </tr>
    <tr>
    <td></td>
    <td> New User? <a href="register.jsp">Register</a> </td>
    </tr>
    </table>

    </form>









    </td>
    </tr>
    <tr>
    <td height="5" colspan="3" valign="top" ><hr/></td>
    </tr>
    <tr>
    <td height="40" colspan="3" valign="top" ><div align="center">The Book Store </div></td>
    </tr>
    </table>
    </body>
    </html>

    </code>
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: problem in running a servlet