Link to home
Start Free TrialLog in
Avatar of LeanMoreTryMore
LeanMoreTryMore

asked on

How do I call from Page1 to page2?

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.

Avatar of Mick Barry
Mick Barry
Flag of Australia image

you can either forward the request to the other page, or redirect it.
to forward:

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

to redirect:

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

Avatar of LeanMoreTryMore
LeanMoreTryMore

ASKER

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
Hi,

Then you need your Servlet to redirect it.

Regards
Dave
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
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
> <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
in your JSP1

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

This will call your JSP2.jsp on submit event.
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?
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)
SOLUTION
Avatar of suprapto45
suprapto45
Flag of Singapore image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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>

ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial