?
Solved

How do I call from Page1 to page2?

Posted on 2005-05-04
16
Medium Priority
?
350 Views
Last Modified: 2010-05-18
I have developed 2 very sample JSP page.
I want JSP1 call JSP2.
Do I need to write a java program running on Servlet? If yes, where to i put?
Would anyone give me very simple program? I'm very beginner of JSP.

0
Comment
Question by:LeanMoreTryMore
  • 8
  • 4
  • 3
  • +1
16 Comments
 
LVL 92

Expert Comment

by:objects
ID: 13932416
you can either forward the request to the other page, or redirect it.
0
 
LVL 92

Expert Comment

by:objects
ID: 13932420
to forward:

<jsp:forward page="b.jsp"/>

to redirect:

<%
response.sendRedirect("b.jsp");
%>

0
 
LVL 92

Expert Comment

by:objects
ID: 13932428
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 92

Expert Comment

by:objects
ID: 13932431
0
 
LVL 92

Expert Comment

by:objects
ID: 13932480
0
 

Author Comment

by:LeanMoreTryMore
ID: 13932484
This is not quite what I expect.

I want where the submit button is clicked, it then calls the page2. I read some book, they all metioned a java program needs to be run on Servlet to extends AbstractHttpServlet something like that.

See my code below
JSP1
===
<%@ taglib uri="http://java.sun.com/jstl/sql" prefix="sql"%>
<%@ page contentType="text/html; charset=iso-8859-1" language="java" import="java.sql.*, AAPL.*" errorPage="" %>
<P>
  <%
    Database db = new Database();
    db.makeConnection();
    Connection con = db.getConnection();
    Statement stmt = con.createStatement();
    String EmployeePositionNo = null;
    ResultSet EmpPositionRS = stmt.executeQuery("SELECT POSITION_NO FROM PY_POSITIONS WHERE EMPLOYEE_NO = 'DRN2000'");
    if (EmpPositionRS !=null) {
       while (EmpPositionRS.next()) {
         EmployeePositionNo = EmpPositionRS.getString("POSITION_NO");
       }  
    }  
    System.out.println("EmployeePositionNo:" + EmployeePositionNo);
    ResultSet EmpRS = stmt.executeQuery("SELECT EMPLOYEE_NO, SURNAME, GIVEN_NAMES FROM PY_EMPLOYEE WHERE REPORT_TO = '" + EmployeePositionNo + "'");
%>
</P>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Employee Management</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta http-equiv="PRAGMA" value="NO-CACHE">
<DIV align="right">
  <img src="image/img_crm.jpg" width="360" height="70"/>
</DIV>
</head>
<BODY>

<!-- STEP THREE: Copy this code into the BODY of your HTML document  -->

<!-- It's important that you position the menu over a background, like a table/image -->
<table bgcolor="#006666" width="100%" border="0" cellpadding="0" cellspacing="0">
<tr><td height="20"><font size="1"> </font>
</td></tr></table>

<P class="style3">
  <STRONG>Current Outstanding Leave Requests</STRONG>
</P>

<table width="85%"  border="1" align="center">
      <tr>
        <td width="13%" valign="top" bgcolor="#ECECEC"><strong>Employee Number </strong></td>
        <td width="47%" valign="top" bgcolor="#ECECEC"><strong>Surname</strong></td>
        <td width="15%" bgcolor="#ECECEC"><strong>Given Names </strong></td>
      </tr>  
<%
if (EmpRS !=null) {
   while (EmpRS.next()) {
%>
   <tr>
<%  
     int counter = 1;
     String empNo = EmpRS.getString("EMPLOYEE_NO");
     String empSurname = EmpRS.getString("SURNAME");
     String empGivenNames = EmpRS.getString("GIVEN_NAMES");
     out.println("<td bgcolor='ECECEC'>" + empNo + "</td>");
     out.println("<td bgcolor='ECECEC'>" + empSurname + "</td>");
     out.println("<td bgcolor='ECECEC'>" + empGivenNames + "</td>");
%>
    </tr>
<%    
  }
}
%>
</table>
<form>
<form name="form1" method="post" action="page1_action.jsp" >    <========= call page1_action.jsp *****
  <input type="submit" name="Submit" value="Submit" onClick="this.style.backgroundColor = '#00ff00;'">
</form>
</body>
</html>
=============================================================================


page1_action.jsp

=============================================================================
<%@ taglib uri="http://java.sun.com/jstl/sql" prefix="sql"%>
<%@ page contentType="text/html; charset=iso-8859-1" language="java" import="java.sql.*, AAPL.*" errorPage="" %>
<P>
<%
  String param1 = request.getParameter("requestor");
%>
  <%
    Database db = new Database();
    db.makeConnection();
    Connection con = db.getConnection();
    Statement stmt = con.createStatement();
    String EmployeePositionNo = null;
    ResultSet EmpPositionRS = stmt.executeQuery("SELECT POSITION_NO FROM PY_POSITIONS WHERE EMPLOYEE_NO = 'DRN2000'");
    if (EmpPositionRS !=null) {
       while (EmpPositionRS.next()) {
         EmployeePositionNo = EmpPositionRS.getString("POSITION_NO");
       }  
    }  
    System.out.println("EmployeePositionNo:" + EmployeePositionNo);
    ResultSet EmpRS = stmt.executeQuery("SELECT EMPLOYEE_NO, SURNAME, GIVEN_NAMES FROM PY_EMPLOYEE WHERE REPORT_TO = '" + EmployeePositionNo + "'");
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Employee Management</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta http-equiv="PRAGMA" value="NO-CACHE">
<DIV align="right">
  <img src="image/img_crm.jpg" width="360" height="70"/>
</DIV>
</P>The email has been send to YOU shortly
</head>


===========================================================================
Thanks for the help
0
 
LVL 16

Expert Comment

by:suprapto45
ID: 13932492
Hi,

Then you need your Servlet to redirect it.

Regards
Dave
0
 
LVL 16

Expert Comment

by:suprapto45
ID: 13932513
Hi,

Normally, what we do in web application is this.

Assuming that the application for login. You have username and password textboxes and submit button.
Now, you have your login.jsp page with the two textboxes and submit button. It will look something like....

<html>
..
<form action="MyServlet1">
<input type="text" name="username" ..>
<input type="password" name="password"..>
<input type="submit" value="submit"..>
</form>
..
</html>

Then, pay attention to your MyServlet1, in here, you will have a servlet (Java class) that will get the username and password, do all the necessary things and redirect the user whether it fails or successes.

public class MyServlet1 extends HttpServlet
{
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // All your logics here
        String username = request.getParameter("username").toString();
        String password = request.getParameter("password").toString();

        if (login == true)
        {
            response.sendRedirect("success.jsp");
        }
        else
        {
            response.sendRedirect("fail.jsp");
        }

    }
}

Does that help you?

Regards
Dave
0
 
LVL 16

Expert Comment

by:suprapto45
ID: 13932519
Hi,

Remember that for your Servlet, you need to map it into your web.xml.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
                         "http://java.sun.com/dtd/web-app_2_3.dtd">
<!-- Copyright (c) 2002 by ObjectLearn. All Rights Reserved. -->
<web-app>

    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
    <error-page>
        <error-code>404</error-code>
        <location>/error.jsp</location>
    </error-page>  
     
    <servlet>
        <servlet-name>MyServlet1</servlet-name>
        <servlet-class>com.yourpackage.MyServlet1</servlet-class>
    </servlet>
 
       
     <servlet-mapping>
        <servlet-name>MyServlet1</servlet-name>
        <url-pattern>/MyServlet1</url-pattern>
    </servlet-mapping>

</web-app>

In your case, I think that all the scriptlets <% %> can be placed in your Servlet. it helps you to separate your business and presentation layer.

regards
Dave
0
 
LVL 92

Expert Comment

by:objects
ID: 13932532
> <form>

get rid of that line

> <form name="form1" method="post" action="page1_action.jsp" >    <========= call page1_action.jsp *****

that should be ok, what happens when you submit the form.

>   String param1 = request.getParameter("requestor");

Your form does not have a parameter named requestor that I can see
0
 
LVL 11

Expert Comment

by:raj3060
ID: 13937105
in your JSP1

<form name="form1" onsubmit="JSP2.jsp">
--
--
</form>

This will call your JSP2.jsp on submit event.
0
 

Author Comment

by:LeanMoreTryMore
ID: 13941111
I'm very confused about using Servlet.

Does all the action page, eg. JSP2.jsp, need to be implemented in Servlet? If not, how to i determine whether to use JSP page or Java Servlet? I hope you all understand my question as I really dont know why I need to use Servlet.

Can I build the web application just using JSP?
0
 
LVL 92

Expert Comment

by:objects
ID: 13941147
Theres no need to use a servlet, you can implement your action page as either a jsp or a servlet.
(jsp's get compiled into servlets)
0
 
LVL 16

Assisted Solution

by:suprapto45
suprapto45 earned 600 total points
ID: 13941583
Hi,

>>"Can I build the web application just using JSP?"
Yes, why not? But you cannot separate the business and presentation layer. Simply, it means that you will mix all your business logics (calculations or etc) and your HTML/JSP together.

The advantage of using Servlet is that you can put all your calculations and business logics in separate place (Servlet) and later on, if you want to modify this class, it should be easy for you. In addition Servlet has some additional advantages such as it provides you with rich listener to make your life easier.

Regards
Dave
0
 

Author Comment

by:LeanMoreTryMore
ID: 13942445
Why it does not call another page as I use onsubmit

See my code

<form name="form1"  onsubmit="JSP2.jsp">
<P class="style3">
  <STRONG>Current Outstanding Leave Requests</STRONG>
</P>

<table width="85%"  border="1" align="center">
      <tr>
        <td width="13%" valign="top" bgcolor="#ECECEC"><strong>Employee Number </strong></td>
        <td width="47%" valign="top" bgcolor="#ECECEC"><strong>Surname</strong></td>
        <td width="15%" bgcolor="#ECECEC"><strong>Given Names </strong></td>
      </tr>  
<%
if (EmpRS !=null) {
   while (EmpRS.next()) {
%>
   <tr>
<%  
     int counter = 1;
     String empNo = EmpRS.getString("EMPLOYEE_NO");
     String empSurname = EmpRS.getString("SURNAME");
     String empGivenNames = EmpRS.getString("GIVEN_NAMES");
     out.println("<td bgcolor='ECECEC'>" + empNo + "</td>");
     out.println("<td bgcolor='ECECEC'>" + empSurname + "</td>");
     out.println("<td bgcolor='ECECEC'>" + empGivenNames + "</td>");
%>
    </tr>
<%    
  }
}
%>
</table>
  <input type="submit" name="Submit" value="Submit" onClick="this.style.backgroundColor = '#00ff00;'">
</form>
</body>
</html>

0
 
LVL 92

Accepted Solution

by:
objects earned 600 total points
ID: 13942452
> <form name="form1"  onsubmit="JSP2.jsp">

shouyld be action, not onsubmit

<form name="form1"  action="JSP2.jsp">
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Applications for our next round of the Experts Exchange Scholarship Contest are starting to roll in. It made us wonder what our past winners are up to these days. Here's a look at what four winners experienced with the contest and what they're doing…
In the below post we have mentioned the best hosting type for startups. Also, check out some of the superlative web hosting companies that are proposing affordable web hosting solutions to host your startup website.
Integration Management Part 2
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses
Course of the Month16 days, 17 hours left to enroll

864 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