Html link to a servlet

Hello,

I am new to Java Servlet programming and I need some help with calling a servlet via a HTML link.

I have a class which generates a html page:

package HccMembers;

public class HccMemberServlet

{

public StringBuffer buffer;

public HccMemberServlet(String sHccId, String sHcId)

{

buffer = new StringBuffer(4096);

this.buffer.append("<HTML>\n ");

this.buffer.append("<HEAD>\n ");

this.buffer.append("<TITLE>Members</TITLE>\n");

this.buffer.append("</HEAD> \n");

this.buffer.append("<BODY> \n");

this.buffer.append("<p><ahref='http://localhost:8080/transport/HccMembers/servlet/JCSJobOrderServlet'>New Job Order</a></p>");

this.buffer.append("</BODY></HTML>");

}

Basically, when the user clicks the link a servlet should be invoked. (Below)

package HccMembers;

//import java Servlet classes

import javax.servlet.*;

import javax.servlet.http.*;

//import java classes

import java.io.IOException;

import java.io.*;

import java.util.*;

import java.sql.*;

public class JCSJobOrderServlet extends HttpServlet

{

public void init(ServletConfig config) throws ServletException

{

{

System.out.println("hello");

}

}

public void service(HttpServletRequest request, HttpServletResponse response)

throws IOException, ServletException

{

System.out.println("Hey");

}

}

The problem is that, when I click the link I get a 404 page not found error. Any ideas?

I really appreciate any help.

[1756 byte] By [dh_1] at [2007-9-26 4:46:12]
# 1
There are a few things that can go wrong... like for instance is the servlet really where the link href points to?
jsalonen at 2007-6-29 18:35:18 > top of Java-index,Archived Forums,Java Programming...
# 2
Yes, the Servlet is in the right folder.What other things can go wrong?regards
dh_1 at 2007-6-29 18:35:18 > top of Java-index,Archived Forums,Java Programming...
# 3
... and the thing works when you type the address in the browser location bar?
jsalonen at 2007-6-29 18:35:18 > top of Java-index,Archived Forums,Java Programming...
# 4
Are you certain the file is where the link is pointing?Try changing the link to http://localhost:8080/transport/servlet/HccMembers/JCSJobOrderServlet
mattbunch at 2007-6-29 18:35:18 > top of Java-index,Archived Forums,Java Programming...
# 5

It seems you are tryng to invoke the servlet as such, i.e. without having any alias mapped to it.

If this is right, and the servlet container you are using supports this, then you have to provide the fully qualified class name of your servlet in the URL that invokes the servlet...

http://localhost:8080/transport/servlet/HccMembers.JCSJobOrderServlet

neville_sequeira at 2007-6-29 18:35:18 > top of Java-index,Archived Forums,Java Programming...
# 6

The url doesn't need to have the protocol and host. Think that when you deploy this application the users wan't be able to access this 'localhost:8080'.

So, construct the url that way:

/web_context/servlet/package.servletclass

In your case this should be:

/transport/servlet/HccMembers.JCSJobOrderServlet

llturro at 2007-6-29 18:35:18 > top of Java-index,Archived Forums,Java Programming...
# 7

You need to add couples of lines to the configuration file of your application (web.xml) : first you must say that the class CSJobOrderServlet is a servlet that you want to use, second you must map the servlet to a specific url, then you can use this url whenever you like.

As an advice, don't use absolute URLs on your application, use only URLs relative to your application root.

Example for web.xml:

<servlet>

<servlet-name> MyFirstServlet</servlet-name>

<servlet-class>JCSJobOrderServlet</servlet-class>

</servlet>

<servlet-mapping>

<servlet-name>MyFirstServlet</servlet-name>

<url-pattern>/custom_url</url-pattern>

</servlet-mapping>

Now, about the servlet implementation: if you are using System.out.println("Hey") you will see the message on the standard output of the server, and not on the browser as you expected.

here is a small example:

public void doGet(HttpServletRequest request,

HttpServletResponse response)

throws IOException, ServletException {

response.setContentType("text/html");

PrintWriter writer = response.getWriter();

writer.println("<html>");

writer.println("<head><title>Hello response</title></head>");

writer.println("<body>Hello !</body></html>");

}

For more information see the documentation for servlets and / or a book.

Iulian

iulian_musat at 2007-6-29 18:35:18 > top of Java-index,Archived Forums,Java Programming...
# 8
Hi all, Thanks 4 your help, I will try the different suggests. Answer to: jsalonen, "... and the thing works when you type the address in the browser location bar? " No it doesnt work. best regards
dh_1 at 2007-6-29 18:35:18 > top of Java-index,Archived Forums,Java Programming...
# 9
Hi, when u speak of alias mapping, "neville_sequeira,It seems you are tryng to invoke the servlet as such, i.e. without having any alias mapped to it." Is this when u change the web.xml has iulian_musat suggests?thanks
dh_1 at 2007-6-29 18:35:18 > top of Java-index,Archived Forums,Java Programming...
# 10
this is problem due to wrong alias name. check it.
alp_patel at 2007-6-29 18:35:18 > top of Java-index,Archived Forums,Java Programming...
# 11

When using Tomcat standalone, with Apache or in J2EE isn't necessary to use servlet-mapping. Every servlet placed in WEB-INF/classes will be directed to the servlet/ path by default. This makes your path longer, that is:

web_context/servlet/package/yourServlet

instead of:

web_context/yourMapping

as will be with servlet-mapping. But anyway it works!

llturro at 2007-6-29 18:35:18 > top of Java-index,Archived Forums,Java Programming...