Calling a jsp page from Servlet

Calling a jsp page from Servlet

How can I do this? Suppose I have jsp page aaa.jsp. From aaa.jsp on form action I have made a call to a servlet xxx.java. In xxx.java I have written code to retrieve data from database through List and data is being retrieve.

Now I want to pass this List to another jsp page bbb.jsp from xxx.java. How can I do this? How can I get the accurate path of the bbb.jsp? To be frankly, managing path is one of the great headache to me. Please help me.

View Answers

May 20, 2011 at 5:23 PM

1)callServlet.jsp:

<form method="post" action="../Servlet">
<input type="submit" value="Call Servlet">
</form>

2)Put the servlet in the classes folder and do the servlet mapping in web.xml.Here is Servlet.java:

import java.io.*;
import java.sql.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class Servlet extends HttpServlet{
    protected void doPost(HttpServletRequest req, HttpServletResponse res)
        throws ServletException, IOException{

ArrayList list=new ArrayList();
try{
     Class.forName("com.mysql.jdbc.Driver");
           Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "root");
           Statement st=con.createStatement();
           ResultSet rs=st.executeQuery("select * from employee");
           while(rs.next()){
               list.add(rs.getString("name"));
               list.add(rs.getString("address"));
               list.add(rs.getString("contactNo"));
               list.add(rs.getString("email"));
}
req.setAttribute("data", list);
        RequestDispatcher rd = req.getRequestDispatcher("/jsp/data.jsp");
        rd.forward(req, res);
}
catch(Exception e){
}
    }
}

3)data.jsp:

<%@page language="java" import="java.util.*" %>
<html>
<body> 
<table border="1" width="303">
<tr>
<td width="119"><b>Name</b></td>
<td width="168"><b>Address</b></td>
<td width="119"><b>Contact no</b></td>
<td width="168"><b>Email</b></td>
</tr>
<% Iterator itr;%>
<% List data=(List)request.getAttribute("data");
for(itr=data.iterator(); itr.hasNext(); ){
%>
<tr>
<td width="119"><%=itr.next()%></td>
<td width="168"><%=itr.next()%></td>
<td width="168"><%=itr.next()%></td>
<td width="168"><%=itr.next()%></td>
</tr>
<%}%>
</table>
</body>
</html>

May 20, 2011 at 5:24 PM

For more information, visit the following link:

Servlet Turorials









Related Tutorials/Questions & Answers:
Calling a jsp page from Servlet
Calling a jsp page from Servlet  How can I do this? Suppose I have jsp page aaa.jsp. From aaa.jsp on form action I have made a call to a servlet... List and data is being retrieve. Now I want to pass this List to another jsp page
calling servlet from jsp
calling servlet from jsp  how to call a servlet from jsp
Advertisements
calling one jsp from another jsp page
calling one jsp from another jsp page  need coding for calling one jsp from another jsp including the xml file.Its urgent
calling servlet from jsp in netbeans ide
calling servlet from jsp in netbeans ide  I have tried to call servlet from jsp code in netbeans for checking the database values . but while running it showing the error that the resource not available. i dono wat mistake i did
calling one jsp from another jsp - JSP-Servlet
calling one jsp from another jsp  Hi All, In my web application... in two.jsp by using jsp declarative tag. Now from one.jsp file I want to call... pageContext.include("./nextjsppage.jsp"); when you use this statement the jsp page
Calling servlet from servlet .
Calling servlet from servlet .  How to call a servlet from another... ServletException, IOException { System.out.println("Calling another servlet by using...); System.out.println("Calling another servlet by using SendRedirect
calling one jap page from another jsp page
calling one jap page from another jsp page  i created a button in one jsp page i need to call another jsp page as an action to that button. so how can i call.. plz any one explain. its urgent
Calling from JSP
Calling from JSP  how can we call a class file from JSP   Hi, Do you want to call a servlet or a Java Bean through JSP? Please clarify this. For more information, visit the following link: JSP Tutorials Thanks
calling function - JSP-Servlet
calling function  Hai, How to call a java file in jsp file... : 1.Create a jsp page "test.jsp" Example of Extends Attribute of page Directive in JSP 2.Use "import" attribute in JSP
Calling Servlet to build a List of data from database and show this on the JSP page in table
Calling Servlet to build a List of data from database and show this on the JSP... is then added to the request object and sen to JSP page. On the JSP page this value... to the JSP page. We are using tomcat to run and test the applicationADS_TO_REPLACE_1
calling servlet from JS and return response from servlet to JS
calling servlet from JS and return response from servlet to JS  hello all, I am working on JSP-servlets n MVC.I am facing a problem. on the web page when user clicks on a button i am calling a javascript function(eg myFunc
Parameter passing from jsp page to jsp page - JSP-Servlet
Parameter passing from jsp page to jsp page  Hi I intends to pass the parameters that I received from previous page through query String. I get all... Welcome :  For more information on JSP visit
calling java beans - JSP-Servlet
calling java beans  Sir, I want to know where to place the java beans java file and class file inside tomcat web server. and how to call them from jsp file.  Hi Friend, Java Bean is placed in classes\form
Fetching database field from servlet to jsp page ?
Fetching database field from servlet to jsp page ?  Hello Java... field. I wanted to pass some of the database field from servlet to jsp... (i...) field to jsp page . { where 'rs' is Resultset object} please help
How to pass parametes from JSP page to HTML page? - JSP-Servlet
How to pass parametes from JSP page to HTML page?  Hi all, In my project I have one JSP page and one HTML page. In JSP page I have created HTML... to pass the values from my JSP page to HTML page. Pls help me out. Hitendra 
calling zipping function - JSP-Servlet
calling zipping function  Hi, Is it possible to zip a jsp file. I wrote a java function for zipping a file.How to call that java file (zipping function ) in JSP ? pls help me Thanks.... 
Sending form data from HTML page to SQLserver 2005 database by calling servlet code
Sending form data from HTML page to SQLserver 2005 database by calling servlet... from html page to database by calling servlet code from html page .   ... the servlet mapping in web.xml: <servlet> <servlet-name>
unable to display table data on JSP page that is coming from mysql and servlet.
unable to display table data on JSP page that is coming from mysql and servlet.  I am unable to show table data on JSP page using servlet and mysql. only two rows data i showing but in my database I have five fields
unable to display table data on JSP page that is coming from mysql and servlet.
unable to display table data on JSP page that is coming from mysql and servlet.  I am unable to show table data on JSP page using servlet and mysql. only two rows data i showing but in my database I have five fields
unable to display table data on JSP page that is coming from mysql and servlet.
unable to display table data on JSP page that is coming from mysql and servlet.  I am unable to show table data on JSP page using servlet and mysql. only two rows data i showing but in my database I have five fields
calling java method from html form with out using javascript - JSP-Servlet
calling java method from html form with out using javascript  How can i call java method from a HTML form, java script should be disabled?  Hi <html><head><title>Test Input Validation</title><
Java from JSP - JSP-Servlet
Calling Java from JSP  Does anyone have an example of Calling Java from JSP
How to display Jfreechart from servlet in jsp web page at specified location
How to display Jfreechart from servlet in jsp web page at specified... in jsp web page . Thank you very much Sir... to display the chart in web page. I generated the chart using Jfreechart
Navigate from jsp to servlet - JSP-Servlet
Navigate from jsp to servlet   Hi Friends, Sample code for Navigating a page from jsp to servlet. Thanks
jsp page - JSP-Servlet
jsp page  what are elemets and template data in jsp page??  Hi Friend, Template Data is a parameterized content that allows the JSP document to generate a response stream of characters. JSP Elements provides
calling a session bean bean from servlet using netbeans - EJB
calling a session bean from servlet using netbeans  How to call a session bean from servlet using netbeans in Java
how to add Arraylist filter for a jsp page showing results from a servlet
how to add Arraylist filter for a jsp page showing results from a servlet  hello sir/mam, in my project i have an arraylist showing results in a jsp..._TO_REPLACE_1 Please visit the following link: http://roseindia.net/jsp/servlet-jsp-data
ArrayList from JSP to Servlet - JSP-Servlet
ArrayList from JSP to Servlet  Hi, I have an arraylist declared in a scriplet in a jsp page. how can i access the arraylist in a servlet which... should it be declared in jsp and in servlet. do i need to do any casting
Pass a dom object from jsp to servlet
Pass a dom object from jsp to servlet  I am creating a dom object in my jsp page. now i want to pass that object in a servlet through calling servlet in jsp. can anyone help me
Values from servlet into dropdownlist in jsp page using ajax
Values from servlet into dropdownlist in jsp page using ajax  1) jsp file <%@page import="java.sql.Statement"%> <%@page import... servlet into dropdownlist of jsp page...kindly check the code attached and tell me
How to set the pdfptable as pdf page header when generating the pdf from jsp page. - JSP-Servlet
How to set the pdfptable as pdf page header when generating the pdf from jsp page.  how to set the pdfptable column headers as a header of the documet.i need to display the column headers for every pdf page if pdf documt is 20
Call servlet from javascript - JSP-Servlet
Call servlet from javascript  Hi in my application i have jsp that designs view, javascript for validation and servlet that perform business logic.../jqueryPostData.shtml Thanks    i am calling servlet in this manner var answer2
The type Font is ambiguous where generating pdf file from jsp page - JSP-Servlet
The type Font is ambiguous where generating pdf file from jsp page  I am geeting ambiguous error .Do u want to add any jar files   Hi Friend, Import the class Font into your JSP page like the following
Pass parameters from JSP to Servlet
Pass parameters from JSP to Servlet In this section, you will learn how to pass parameters from JSP page to servlet. For this purpose, we have used... is retrieved later in the servlet by passing the request object through
Sorting a jsp page. - JSP-Servlet
Sorting a jsp page.  Hi there, It's me again. I tried to use Ajax... is process the code in the servlet. Here's the code in the jsp below: JSP Page Universal Books Home Page
Page object - JSP-Servlet
Page object  Hello friends, How can we make use of PAGE object of implicit JSP object. If this is possible explain me about.......  Hi friend, Implicit Objects in JSP are objects
JSP Hindi Page - JSP-Servlet
JSP Hindi Page  I need to provide users with option to print a Certificate in English or Hindi. This is an html page generated by JSP Code. Depending... be shown. Dynamic data will be obtained from data based and will be in english. So
calling a web page when pressing on a Button - JSP-Interview Questions
calling a web page when pressing on a Button  Please perhaps I do... and Update. When the user clicks on a button View, I call a web page When the user clicks on a Button Print, I call another web page, When the user clicks on Update
how to pass variable from simple java main class(not servlet) to the jsp page?
how to pass variable from simple java main class(not servlet) to the jsp page?  I have a simple Java class in which I invoke a call to a JSP page... from the simple Java class to the JSP page. How can I do that? Here is my
export jsp page - JSP-Servlet
export jsp page  i want to export jsp page in word and pdf format... code that export my jsp page in word and pfd format....  Hi Friend, Try the following code: 1)pdf.jsp: Name Address Contact
JSP error page - JSP-Servlet
JSP error page  Hi i have 1000 of JSP. but we coded it without adding the . but now i have to forward the page to the specific page when the exception occured in JSP. is there any short cut i can do? do not want to add this line
The type Font is ambiguous where generating pdf file from jsp page - JSP-Servlet
The type Font is ambiguous where generating pdf file from jsp page  i am getting the type font is ambiguous and type imgae ambiguous ? document.open(); 32: Font headFont = FontFactory.getFont(FontFactory.HELVETICA
jsp page tags - JSP-Servlet
jsp page tags  Hi, Can anyone please explain me how to use Pager tags (http://jsptags.com/tags/navigation/pager/index.jsp). After getting the data from the database, how should we use that in these tags. Can you please
write to file from servlet - JSP-Servlet
write to file from servlet  Hi, I have a jsp file where I input... JSP Page //<... page is: " + name + ""); out.println(" My Social security from
page refresh - JSP-Servlet
page refresh  Hi All, I've done a login application using Ajax. I wrote jsp page having sign in option. when i click the sign in,pop up window... up window has to close and jsp page has to refresh. how can i solve
How to download files from server to local disk using ZIP format in JSP page - JSP-Servlet
How to download files from server to local disk using ZIP format in JSP page  hi i have an application to upload and download multiple files. As i can upload multiple files .now i have to download all uploaded files on local
how to add the scrollbar to the pdf page when generating the pdf file from jsp - JSP-Servlet
how to add the scrollbar to the pdf page when generating the pdf file from jsp  I am not able to see all the columns when i generated the pdf file from jsp.i have 12 colums so how to add the scrollbar
How to get jSon object in servlet from jsp - JSP-Servlet
How to get jSon object in servlet from jsp  How to get jSon object in servlet from jsp? In jsp page i have written: var sel... object in servlet???  Hi Friend, Please visit the following link
printing records from jsp - JSP-Servlet
printing records from jsp  Hi Plz tell me how to printing out pages from jsps with page numbers Thank you  Hi friend, For paging in Jsp visit to : http://www.roseindia.net/jsp/paging.shtml http
regarding-update the Jsp page itself - JSP-Servlet
regarding-update the Jsp page itself   Joined: Aug 17, 2009 Messages: 12 [Post New]posted Today 10:39:52 PM Quote Edit [Up] I have two Jsp... is i want to update the information on same Jsp page. How can I implement

Ads