Servlet Behaviour

I am testing my first servlet and have strange result.

I have a simple html page called start.html

<HTML>

<HEAD>

<TITLE> Test Servlet </TITLE>

<META NAME="Generator" CONTENT="EditPlus">

<META NAME="Author" CONTENT="">

<META NAME="Keywords" CONTENT="">

<META NAME="Description" CONTENT="">

</HEAD>

<BODY>

<FORM METHOD="POST" ACTION="http://localhost:8080/testservlet">

Please Enter your Name

<INPUT TYPE="text" NAME="contact">

<INPUT TYPE="submit" value="Submit">

</FORM>

</BODY>

</HTML>

I've created the servlet called testservlet.java and here's the code for the servlet.

import javax.servlet.*;

import javax.servlet.http.*;

import java.io.*;

import java.util.*;

import java.lang.Object.*;

publicclass TestServletextends HttpServlet

{

publicvoid init(ServletConfig config)

throws ServletException

{

super.init(config);

}

publicvoid doGet(HttpServletRequest req,

HttpServletResponse resp)

throws ServletException, java.io.IOException

{

doPost(req, resp);

}

publicvoid doPost(HttpServletRequest req,

HttpServletResponse resp)

throws ServletException,

java.io.IOException

{

/** set content type and get writer */

resp.setContentType("text/html");

PrintWriter out=resp.getWriter();

String contact=req.getParameter("contact");

out.println(GenerateOutput(contact));

out.close();

}

private String GenerateOutput(String contact)

{

StringBuilder sb=new StringBuilder();

sb.append("<html><body><head><title>Test Servlet response</title></head>");

sb.append("

Your Name is " + contact +"

");

sb.append("</body></html>");

return sb.toString() ;

}

publicvoid destroy(){

super.destroy();

}

}

I've deployed the servlet to the Sun Application Server.

If I run the html page and enter the name servlet returns the list of file in the testservlet directory instead of the correct output. It looks like this.

http://localhost:8080/testservlet/

Directory Listing for

Directory Filename Size

What's wrong ?

Thanks

Mikhail

[4117 byte] By [mgoncharova] at [2007-10-3 0:32:20]
# 1

you didn't write the servlet properly. give it a package.

you didn't deploy it properly. do you have a context with a WEB-INF/classes directory? If not, create one.do you have a web.xml that maps the servlet to a URL? if not, write one.

you shouldn't hardwire the name of the server in the page. that won't work if you deploy it anywhere else.

maybe a book or tutorial can help.

%

duffymoa at 2007-7-14 17:25:43 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2

1. Is the package required for the servlet to work ? The example which I've seen are not using the package.

2. I have deployed the WEB-INF folder this way

WEB-INF

/classes

TestServlet.class

web.xml

sun-web.xml

here's the code for the web.xml file

<?xml version="1.0" encoding="UTF-8"?>

<!--

Copyright 2004-2005 Sun Microsystems, Inc. All rights reserved.

Use is subject to license terms.

-->

<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

<display-name>testservlet</display-name>

<distributable/>

</web-app>

here's the code for the sun-web.xml file

<?xml version="1.0" encoding="UTF-8"?>

<!--

Copyright 2004-2005 Sun Microsystems, Inc. All rights reserved.

Use is subject to license terms.

-->

<!DOCTYPE sun-web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Application Server 8.1 Servlet 2.4//EN" "http://www.sun.com/software/appserver/dtds/sun-web-app_2_4-1.dtd">

<sun-web-app>

<context-root>/testservlet</context-root>

</sun-web-app>

The servlet was deployed successfully on the Sun Application Server 9 using the Web Application Deployment admin.

3. This is done for testing only, I understand that

Mikhail

mgoncharova at 2007-7-14 17:25:44 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3

> 1. Is the package required for the servlet to work ?

> The example which I've seen are not using the

> package.

In general, I believe the answer is yes. You might be looking at old examples.

In Tomcat I know that packages are required.

> 2. I have deployed the WEB-INF folder this way

> > WEB-INF

> /classes

> TestServlet.class

> web.xml

> sun-web.xml

>

>

How old ARE those examples? What's in that sun-web.xml? You should not need any extensions to deploy a simple servlet like this.

> here's the code for the web.xml file

>

> >

> <?xml version="1.0" encoding="UTF-8"?>

>

> <!--

> Copyright 2004-2005 Sun Microsystems, Inc. All

> rights reserved.

> Use is subject to license terms.

> -->

>

> <web-app version="2.4"

> xmlns="http://java.sun.com/xml/ns/j2ee"

> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

> xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee

> http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

><display-name>testservlet</display-name>

> <distributable/>

> </web-app>

>

>

This is utterly wrong. You didn't map your servlet.

> here's the code for the sun-web.xml file

>

> >

> <?xml version="1.0" encoding="UTF-8"?>

>

> <!--

> Copyright 2004-2005 Sun Microsystems, Inc. All

> rights reserved.

> Use is subject to license terms.

> -->

>

> <!DOCTYPE sun-web-app PUBLIC "-//Sun Microsystems,

> Inc.//DTD Application Server 8.1 Servlet 2.4//EN"

> "http://www.sun.com/software/appserver/dtds/sun-web-ap

> p_2_4-1.dtd">

> <sun-web-app>

><context-root>/testservlet</context-root>

> sun-web-app>

>

>

You should not need this.

> The servlet was deployed successfully on the Sun

> Application Server 9 using the Web Application

> Deployment admin.

"successfully"?Then why are you here?

> 3. This is done for testing only, I understand that

>

> Mikhail

I'd read more about the details of deploying web apps on the Sun app server. Learn how to package it into a WAR file. That'll work, ir you do it properly.

%

duffymoa at 2007-7-14 17:25:44 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...