Hello everyone..

My question is how to display data from my database.
So when I open my index.jsp file, the data already display on it, it doesnt have any form. Is servlet can do it, or just do it in index.jsp file.
I prefer with servlet, but I don't know how to do.

Thanks

You can do it through one of the meta tags inside the head block of html document. This will redirect user in 1 sec to next page. So if you leave it empty visitor should hardly notice

<meta http-equiv="refresh" content="1; url=database.jsp" >

and little masking inside web.xml

<servlet>
       <servlet-name>Database</servlet-name>
       <servlet-class>GetFirstData</servlet-class>
    </servlet>

    <servlet-mapping>
       <servlet-name>Database</servlet-name>
       <url-pattern>/database.jsp</url-pattern>
    </servlet-mapping>

replace "GetFirstData" with name of your real servlet and you done

thanks peter_budo, but it didn't woark as I hope.

For example in index file, I have 2 div :

<div id="leftBox">
</div>
<div id="rightBox">
</div>

In "rightBox" I want to running "myServlet" without any parameter. I already try with <jsp:include page"myServlet">, but still it didn't work or I have to write my html/jsp code in servlet.

Finally, my problem solved.

Thanks again to peter_budo for giving me an example of this

<servlet>
<servlet-name>Database</servlet-name>
<servlet-class>GetFirstData</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>Database</servlet-name>
<url-pattern>/database.jsp</url-pattern>
</servlet-mapping>

I didn't know servlet can mapping with jsp file.

So, here is my step:

  1. Create servlet with a code of sql to view the data in the database.
  2. Use RequestDispatcher to direct the page. In this case I name it showData.jsp.
  3. To get the data I use javabean in showData.jsp
  4. Mapping the servlet into index.jsp

Simple and working good as I hope

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.