Not able to call servlet from jsp
I have a jsp from which i'm calling a servlet code is given below:- <html> <body> <jsp:forward page="/doubleHello" /> </body> </html> my web.xml is:- <web-app> <display-name>HelloServlet</display-name> <description> HelloServlet </description> <servlet> <servlet-name> ServletCallingEjb </servlet-name> <servlet-class> myServlet.HelloServlet </servlet-class> </servlet> <servlet-mapping> <servlet-name> ServletCallingEjb </servlet-name> <url-pattern> /doubleHello </url-pattern> </servlet-mapping> </web-app> My servlet looks like:- package myServlet; import myEjb.*; import java.io.*; import javax.servlet.*; import javax.servlet.http.*; import javax.naming.*; import java.io.PrintWriter; import java.io.IOException; public class HelloServlet extends HttpServlet { public void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.println("<html><head><title>Hello from Servlet</title></head>"); out.println("<body>
Hello from hello servlet!
</body>"); out.println("
"; out.println("</html>"); } } But when i run the code i'm getting page cannot be displayed. Please please suggest what am i doing wrong?
# 1
Hi there,
One problem I can see is , in your web.xml
Why do you have a space before and after the content of an XML node
for example you have a space before and after /doubleHello , remove the spaces and try again.
<url-pattern> /doubleHello </url-pattern>
In the future ,
1) please use the Enter key to add new lines in your message and to add blank links otherwise it is so difficult to read your message.
2) Format your code (indent it) before posting , if everything falls on one line it is very difficult to read.
3) Use the code button , it generates code tags and paste your code inside the code tags - as it provides syntax highlighting and makes it easier for us to read.
# 5
hi ,
your servlet coding is right and JSP also.
but check few steip to call servlet from jsp.
a) define action in jsp is same as servlet display name with "/" .
b)check web.xml file
c) <jsp:forward> is compulsary in your case or not .otherwise use just <html:form method="GET/POST" action="/servlet-name"> in jsp page
and just put this code in your web.xml(As a sample)
<web-app id="WebApp_ID" 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> doubleHello</display-name>
<servlet>
<description></description>
<display-name>doubleHello</display-name>
<servlet-name>doubleHello</servlet-name>
<servlet-class>doubleHello</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>doubleHello</servlet-name>
<url-pattern>/doubleHello</url-pattern>
</servlet-mapping>
I hope it will be help you
# 6
One correction: Instead of
<html:form method="GET/POST" action="/servlet-name">
I would enter it as
<html:form method="GET/POST" action="url-pattern without slash">
For example, if the url-pattern is
<url-pattern>/Upload.do</url-pattern>
The HTML form tag would read:
<html:form method="POST" action="Upload.do">
This works for me.