JSP and Servlets

I'm new to Java, JSP, and servlets. I'm using NetBeans as my development platform with Tomcat as the container. I have a simple JSP page that I'm trying to execute my servlet with. Listed below is both the JSP page and the servlet code. The servlet never seems to be executed. I'm not sure if I've coded it wrong or I have the package setup wrong. When I test the JSP page in NetBeans the only message I receive is that the the document is rendered.

Thanks for the help!!

Gary

--JSP Page -

<%@page contentType="text/html"%>

<html>

<head><title>JSP Page</title></head>

<body>

<jsp:forward page="/EdrTest2" />;

</body>

</html>

Servlet --

/*

* EdrTest2.java

*

* Created on August 1, 2001, 10:00 AM

*/

package Edr;

import javax.servlet.*;

import javax.servlet.http.*;

/**

*

* @author gary_monk

* @version

*/

public class EdrTest2 extends HttpServlet {

/** Initializes the servlet.

*/

public void init(ServletConfig config) throws ServletException {

super.init(config);

}

/** Destroys the servlet.

*/

public void destroy() {

}

/** Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.

* @param request servlet request

* @param response servlet response

*/

protected void processRequest(HttpServletRequest request, HttpServletResponse response)

throws ServletException, java.io.IOException {

response.setContentType("text/html");

java.io.PrintWriter out = response.getWriter();

out.println("<html>");

out.println("<head>");

out.println("<title>Servlet</title>");

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

out.println("<body>");

out.println("This is a test of a servlet");

out.println("</body>");

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

out.close();

}

/** Handles the HTTP <code>GET</code> method.

* @param request servlet request

* @param response servlet response

*/

protected void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, java.io.IOException {

processRequest(request, response);

}

/** Handles the HTTP <code>POST</code> method.

* @param request servlet request

* @param response servlet response

*/

protected void doPost(HttpServletRequest request, HttpServletResponse response)

throws ServletException, java.io.IOException {

processRequest(request, response);

}

/** Returns a short description of the servlet. */

public String getServletInfo() {

return "Short description";

}

}

[2966 byte] By [g_monk] at [2007-9-26 2:39:18]
# 1

You are providing any action in the jsp page. Here action means form action. This "GET" & "POST" works when forms submit action. When action is not provding then user "SERVICE" method in the servlet. I have changed your servlet. Try it.

package Edr;

import javax.servlet.*;

import javax.servlet.http.*;

/**

*

* @author gary_monk

* @version

*/

public class EdrTest2 extends HttpServlet {

/** Initializes the servlet.

*/

public void init(ServletConfig config) throws ServletException {

super.init(config);

}

/** Destroys the servlet.

*/

public void destroy() {

}

/** Processes requests for both HTTP <code>service</code> methods.

* @param request servlet request

* @param response servlet response

*/

protected void processRequest(HttpServletRequest request, HttpServletResponse response)

throws ServletException, java.io.IOException {

response.setContentType("text/html");

java.io.PrintWriter out = response.getWriter();

out.println("<html>");

out.println("<head>");

out.println("<title>Servlet</title>");

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

out.println("<body>");

out.println("This is a test of a servlet");

out.println("</body>");

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

out.close();

}

/** Handles the HTTP <code>service</code> method.

* @param request servlet request

* @param response servlet response

*/

protected void service(HttpServletRequest request, HttpServletResponse response)

throws ServletException, java.io.IOException {

processRequest(request, response);

}

/** Returns a short description of the servlet. */

public String getServletInfo() {

return "Short description";

}

}

sngolla at 2007-6-29 10:11:39 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2
Try to do this:// page teste.htm <form name=fm action="/EdrTest2" method=get><input type=submit></form> And submit this form
jaojao at 2007-6-29 10:11:39 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3

First thanks for the replys.

I tried both of them and I still do not see the message that the servlet should display. I'm executing the page in NetBean. Is this normally a problem? I'm using Tomcat as my container and not ICE. Any thoughts?

Any ideas what I am doing wrong?

Thanks

Gary

g_monk at 2007-6-29 10:11:39 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4
Can you execute "/EdrTest2" directly ?
SuperMMX at 2007-6-29 10:11:39 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 5

Given that you're new to servlets and JSP, I assume you haven't set up any servlet "aliases". If that's the case, then your servlet url looks wrong:

replace

<jsp:forward page="/EdrTest2" />;

with

<jsp:forward page="/servlet/EdrTest2" />

Also, there's no need for the semi-colon at the end of the line.

mattbunch at 2007-6-29 10:11:39 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...