Solved

Running Servlets Using Java Server Pages

Posted on 2004-08-23
23
228 Views
Last Modified: 2010-04-01
Hi im learning servlets and HTML.

Here is a HTML code section i which i wan to run a servlet to display a post i extracted from my mysql database.

<table width="100%" border="0" cellpadding="1" cellspacing="1" bgcolor="#FFFFFF">
  <tr bgcolor="#3399FF" >
    <td width="27%" class="color1">Blog</td>
    <td width="73%" class="color1">
    <jsp:include
    page="/Web/servlet/blog.Show"  
    flush="true"/>

&nbsp;</td>
  </tr>
</table>

My page comes out with only the tables empty. And my post did not show at all.... What i wan to do is use the java server pages to run my servlet and display it in on same page in the table.

Pls help , thx in advance

0
Comment
Question by:kekaudangan
  • 7
  • 6
  • 6
  • +1
23 Comments
 
LVL 3

Expert Comment

by:Gunt
ID: 11876928
Try pointing directly to the servlet address "/Web/servlet/blog.Show" and check that the servlet output is right.
And that should not be HTML code, but JSP code, since you are using a JSP tag.

Good luck.
0
 
LVL 3

Expert Comment

by:Gunt
ID: 11876948
http://www.onjava.com/pub/a/onjava/excerpt/java_cookbook_ch18/index.html?page=8

That link may also have some useful information on what you want to do. The page 8 (which is in the link) is about what you are doing, but you might want to read it complete.

0
 
LVL 1

Author Comment

by:kekaudangan
ID: 11877162
According to the
http://www.onjava.com/pub/a/onjava/excerpt/java_cookbook_ch18/index.html?page=8 link

Using the  <%@include file="/Web/servlet/blog.Show"%>

I keep getting an error

org.apache.jasper.JasperException: /Blog2.jsp(9,1) File "/Web/servlet/blog.Show" not found
      org.apache.jasper.compiler.DefaultErrorHandler.jspError(DefaultErrorHandler.java:39)
      org.apache.jasper.compiler.ErrorDispatcher.dispatch(ErrorDispatcher.java:407)
      org.apache.jasper.compiler.ErrorDispatcher.jspError(ErrorDispatcher.java:90)
      org.apache.jasper.compiler.Parser.processIncludeDirective(Parser.java:339)
      org.apache.jasper.compiler.Parser.parseIncludeDirective(Parser.java:372)
      org.apache.jasper.compiler.Parser.parseDirective(Parser.java:475)
      org.apache.jasper.compiler.Parser.parseElements(Parser.java:1539)
      org.apache.jasper.compiler.Parser.parse(Parser.java:126)
      org.apache.jasper.compiler.ParserController.doParse(ParserController.java:220)
      org.apache.jasper.compiler.ParserController.parse(ParserController.java:101)
      org.apache.jasper.compiler.Compiler.generateJava(Compiler.java:203)
      org.apache.jasper.compiler.Compiler.compile(Compiler.java:470)
      org.apache.jasper.compiler.Compiler.compile(Compiler.java:451)
      org.apache.jasper.compiler.Compiler.compile(Compiler.java:439)
      org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:511)
      org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:295)
      org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:292)
      org.apache.jasper.servlet.JspServlet.service(JspServlet.java:236)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:802)


Im just learning java server pages and its kinda irritating when i can't get a simple servlet to run in html.... ^^
0
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
LVL 27

Expert Comment

by:rrz
ID: 11877191
>org.apache.jasper.JasperException: /Blog2.jsp(9,1) File "/Web/servlet/blog.Show" not found  
Please show us your <servlet> and <servlet-mapping> tags in your context's web.xml for your servlet.
Please show us the complete path to your servlet.
>/Web/servlet/blog.Show  
Is Web the name of your context ?  
rrz
0
 
LVL 27

Expert Comment

by:rrz
ID: 11877230
Please tell us which version of Tomcat you are using ?
0
 
LVL 3

Assisted Solution

by:Gunt
Gunt earned 100 total points
ID: 11877349
By the error, you can tell that Tomcat can not locate the Servlet.
Check that the servlet is working by sending it a request using the address bar, write there the "/Web/servlet/blog.Show" address.

If that works, check that the relative path is correct. Note that jsp:include takes a relative URL, so try putting there the servlet address relative to the JSP address.
For instance, if your JSP is in /webapp/Blog2.jsp, and the servlet is in /webapp/blog.Show, use "./blog.Show" or simply "blog.Show" as the address in the include.

But first of all check that the servlet responds.
0
 
LVL 1

Author Comment

by:kekaudangan
ID: 11878255
To :  rrz@871311
My servlet mapping:>
------------------------------------------------------------------------
<web-app 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"
    version="2.4">

  <display-name>My Blog</display-name>
  <description>
     Simple Blog
  </description>
 
      <context-param>
        <param-name>IAMWEBMASTER</param-name>
        <param-value>wee@hotmail.com</param-value>
        <description>
          The EMAIL address of the administrator to whom questions
          and comments about this application should be addressed.
        </description>
      </context-param>



<!-- JSPC servlet mappings start
    <servlet>
        <servlet-name>Write</servlet-name>
        <servlet-class>blog.Write</servlet-class>
    </servlet>
   
    <servlet>
         <servlet-name>Show</servlet-name>
         <servlet-class>blog.Show</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>Show</servlet-name>
        <url-pattern>/Show</url-pattern>
    </servlet-mapping>
   
     <servlet-mapping>
         <servlet-name>Write</servlet-name>
         <url-pattern>/Write</url-pattern>
    </servlet-mapping>
 
    <servlet-mapping>
    <servlet-name>invoker</servlet-name>
    <url-pattern>/*</url-pattern>
    </servlet-mapping>

JSPC servlet mappings end -->

</web-app>

-----------------------------------------------------------------------
Errmm my servlet blog.Show is running when i type :

http://localhost:8080/Web/servlet/blog.Show

and i didn't use the web.xml file cos i can run it by putting a link in my Blog2.jsp called

<a href = "/Web/servlet/blog.Show">Show Blog</a> <-- this works...


Im using Apache Tomcat 5.0


To: Gunt
My servlet responds, but it still shows the same errors which is the file not found... but i can still run the servlet directly... grrr


Anyway heres my code for my jsp (sry i kept saying html hehe)

****************************************************************
<html><head><title>Web log Entry</title></head><body bgcolor="white">


<table width="100%" border="0" cellpadding="1" cellspacing="1" bgcolor="#FFFFFF">
  <tr bgcolor="#3399FF" >
    <td width="27%" class="color1">Blog</td>
    <td width="73%" class="color1">
      
      <%@include file="/Web/servlet/blog.Show"%>  <-- trying to run the servlet.... and display it in the table
&nbsp;</td>
  </tr>
</table>
<br>

</body>
</html>

My objective is trying to execute the servlet with jsp and display on the same page in the table... It that possible? Or am i doing the wrong way or asking the wrong question? Or can anyone show me an example on how to run a servlet with jsp and display it back in the same page/table?

Sry im really kinda blur now cos haven't been sleeping much with this small prob lol
0
 
LVL 35

Expert Comment

by:TimYates
ID: 11878556
Try

<%@include file="/Show"%>
0
 
LVL 35

Expert Comment

by:TimYates
ID: 11879187
or

<%@include file="/Web/Show"%>
0
 
LVL 1

Author Comment

by:kekaudangan
ID: 11879517
To: TimYates
I dun think that will work cos my servlet is using a package called blog. So if i were to call the servlet i need to use blog.Show...
0
 
LVL 35

Expert Comment

by:TimYates
ID: 11879523
> So if i were to call the servlet i need to use blog.Show...

But you have defined the url-pattern as /Show for that servlet

So http://localhost:8080/Web/Show should do the output from your servlet
0
 
LVL 3

Assisted Solution

by:Gunt
Gunt earned 100 total points
ID: 11879936
That's the point of the mapping:

With <servlet> you associate a servlet name to a class, and with <servlet-mapping>, you map an URL to a servlet name (from which, Tomcat derives the class).

This is because you can have several URL patterns handled by the same servlet (in fact, you can use wildcards in URL patterns).

Try the address TimYates gave you.

Good luck.
0
 
LVL 27

Assisted Solution

by:rrz
rrz earned 350 total points
ID: 11882160
><%@include file="/Web/servlet/blog.Show"%>  <-- trying to run the servlet
No, don't use directive.
Please try  
<jsp:include page='<%=request.getContextPath() + "/Show"%>'/>  
Or if  "Web" is your context then try  
<jsp:include page="/Web/Show"%>"/>

><!-- JSPC servlet mappings start    
>JSPC servlet mappings end -->  
You should uncomment your tags.
<!--  this is a XML comment  -->

Why do you map "invoker" without a <servlet>  tag ?  But you say it works ? So, I guess you forgot to post its <servlet> tag .  Please show us that tag.  
Or did you uncomment the invoker servlet in  Tomcat/conf/web.xml  ? That has security issues and is not recommended.         rrz
0
 
LVL 27

Accepted Solution

by:
rrz earned 350 total points
ID: 11882641
No, I was wrong. It should be context relative.
Please use
<jsp:include page="Show"/>
or
<jsp:include page="/Show"/>
rrz
0
 
LVL 1

Author Comment

by:kekaudangan
ID: 11887030
This is my path for the servlet:
\Tomcat 5.0\webapps\Web\WEB-INF\classes\blog\Show.class

With the other commands i keep getting file not found but
Putting this :
<jsp:include page="/Show"/>

My page comes out but the servlet doesn't seem to be running cos my results did not come out.. Its just an empty table....

Is there any other alternative to get my servlet running?

Emm i think i better give u guys a rough example of what i wanna try to do ...here's the link
http://blog.mypdacafe.com/?u=kekaudangan&x=105&y=9

And guys thanks a lot for helping me out and i really appreciate the help thx again guys.
0
 
LVL 35

Expert Comment

by:TimYates
ID: 11887050
Did you try:

<jsp:include page="Show"/>

?
0
 
LVL 35

Expert Comment

by:TimYates
ID: 11887062
and that link is a blog site...with nothing to do with this on it that I could see...

Or are you trying to write a blog site?
0
 
LVL 27

Expert Comment

by:rrz
ID: 11888328
>Putting this :
<jsp:include page="/Show"/>
My page comes out but the servlet doesn't seem to be running cos my results did not come out..      
What happens when you type Tim's url
http://localhost:8080/Web/Show
into your browser address bar  ?  
Did you change your web.xml ? If so, then post it again.        rrz
0
 
LVL 1

Author Comment

by:kekaudangan
ID: 11893096
TO : TimYates
THe code :
<jsp:include page="Show"/>

My page could be displayed but it didn't run the servlet i suppose cos my Show serlvet did not display anything on my page. Besides the word Blog on my left table.


Yes im trying to write a simple blog site. Im trying to display my post which i stored into my database. But i want to display the post without having to create a link to display my posts. Means when i open the page the post are already there. ^^

To: rrz@871311

The code:
http://localhost:8080/Web/Show
I get an error where the page is not found
And no i did not change my web.xml


This is my Show.java
------------------------------------------------------------------------------------------



package blog;

import java.io.*;
import java.text.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
import java.net.URLEncoder;


public class Show extends HttpServlet {

    private static int numPerPage = 10;
    private PrintWriter out = null;
    private MySQLConnector mydb = null;

    private void print(int start)
    {
        try
        {
           // create a new MySQLConnector object
           mydb = new MySQLConnector();
           // get a valid Statement object from the connector
           Statement stmt = mydb.connect();

            // select the most recent log entries from the db
            ResultSet rs = stmt.executeQuery("SELECT title_1,data_1, DATE_FORMAT(created_1,'%M %d, %Y %H:%i') as nice_date from blog_m order by created_1 desc limit " + start + ", " + numPerPage);
            int counter = 0;
            while (rs.next() )
            // print out the blogs
            {
                out.println("<b>" + rs.getString("nice_date")+
                "</b><br>"+rs.getString("title_1")+ "<br>" + rs.getString("data_1") + "<br><br>");
                counter++;
            }
                        // list next page
            if (counter ==  numPerPage )
            {
                int next = start + numPerPage;
                out.println("<br><br><a href=\"/blog/Show?start=" + next + "\">More</a><br><br>");
            }
            else // out of entries - time to go back to the beginning
            {
                out.println("That's it.  Go back to <a href=\"/Web/servlet/blog.Show\">The Beginning Of The Blogs or</a> <br>");
                  out.println("Return to <a href =\"/Web/Blog.jsp\">main </a><br>");
            }

            // disconnect from mysql
            mydb.disconnect(stmt);
        }
        catch (Exception ex)
        {
            out.println("Error retrieving data: " + ex.toString() + "<br>");
        }
    }

    public void doGet(HttpServletRequest request,
                      HttpServletResponse response)
        throws IOException, ServletException
    {

        response.setContentType("text/html");
        out = response.getWriter();

        // print the HTML header
        out.println("<html><head<title>Java Web log</title></head>");
        out.println("<body bgcolor=\"white\">");
        out.println("<h1> My Web Blog Display</h1>");

        int start;
        try
        {
            start = Integer.parseInt( request.getParameter("start") );
        }
        catch (NumberFormatException ne)
        {
            // bad number. start at zero
            start = 0;
        }
        // call the printEntries method to display log entries
        print(start);

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

    public void doPost(HttpServletRequest request,
                       HttpServletResponse response)
        throws IOException, ServletException
    {
        // goPost just calls goGet
        doGet(request,response);
    }

}
--------------------------------------------------------------------------------------------
0
 
LVL 27

Assisted Solution

by:rrz
rrz earned 350 total points
ID: 11893453
>And no i did not change my web.xml
Do you mean to say that you are using what you posted above here ?
You didn't remove the <!--   and the    -->   ?              rrz
0
 
LVL 1

Author Comment

by:kekaudangan
ID: 11894877
yup i didn't is that wrong? or i should remove the <! -->?
0
 
LVL 1

Author Comment

by:kekaudangan
ID: 11895023
Oh the  Tim's url
http://localhost:8080/Web/Show

works. Sry i didn't notice my tags in the web.xml I removed the <!   --> tags.

Errrm but.. now i can't get my jsp to run... the address is http://localhost:8080/Web/Blog2.jsp

Sry but im kinda lost, wut am i suppose to add in the web.xml to run the jsp?
0
 
LVL 35

Assisted Solution

by:TimYates
TimYates earned 50 total points
ID: 11897364
I *think* you need to comment out the

   <servlet-mapping>
    <servlet-name>invoker</servlet-name>
    <url-pattern>/*</url-pattern>
    </servlet-mapping>

bit in web.xml
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
trouble with PDF encryption using itext-1.3.1 17 84
java cyclic graphs 15 209
sortaSum challenge java 17 171
Is possible to make a jsp script work with oracle wallet? 7 101
Do you use a spreadsheet like Microsoft's Excel?  Have you ever wanted to link out to a non excel file on your computer or network drive?  This is the way I found to do it!
This article outlines the process to identify and resolve account lockout in an Active Directory environment.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question