AF
HomeTagSubmit NotesAsk AnythingLoginSubscribe Us
AF
1. Feel Free to ask and submit anything on Anyforum.in and get satisfactory answer
2. Registration is not compulsory, you can directly login via google or facebook
3. Our Experts are looking for yours ?.



java-jsp: How to transfer one form data to another form?

For example

<html>
<head>
<title>Student Registration</title>
</head>
<body>
<form action= " " method="post">

Student Id:<input type="text" name="sid"/>
Student name: <Input type="text" name="sname"/>

<input type="Submit" value="Next"/>

</form>
</body>
</html>
---------------------------------------------------------

<html>
<head>
<title>Address Registration</title>
</head>
<body>
<form action= " " method="post">

Address:<input type="text" name="adrs"/>
Phone: <Input type="text" name="phone"/>

<input type="submit" value="submit"/>

</form>
</body>
</html>
--------------------------------------------------------

When I fill the student registration form and click the Next button then the data forward to the next page & when I fill the Address Registration form & click the submit button then the data store in to the Oracle data base.

Could you please write the code for above problem as early as possible.





Thanking you.





java x 210
jsp x 32
Posted On : 2016-08-12 16:41:10.0
profile MOHAMMAD SALEEM BASHA - anyforum.in MOHAMMAD SALEEM BASHA
266150
up-rate
5
down-rate

Answers


You can use hidden form fields just set all the parameters as hidden field in second form and finally submit it. See below example:

Let´s create a table by executing below query:

CREATE TABLE "STUDENT"
( "ID" NUMBER,
"NAME" VARCHAR2(4000),
"ADDRESS" VARCHAR2(4000),
"PHONE" VARCHAR2(4000)
)
/

index.jsp:
----------------------------------
<html>
<head>
<title>Student Registration</title>
</head>
<body>
<form action= "address.jsp" method="post">
Student Id:<input type="text" name="sid"/>
Student name: <Input type="text" name="sname"/>
<input type="Submit" value="Next"/>
</form>
</body>
</html>

address.jsp:
----------------------------------------
<html>
<head>
<title>Address Registration</title>
</head>
<body>
<form action= "register" method="post">
<input type="hidden" name="sid" value="<%=request.getParameter("sid")%>"/>
<input type="hidden" name="sname" value="<%=request.getParameter("sname")%>"/>
Address:<input type="text" name="adrs"/>
Phone: <Input type="text" name="phone"/>
<input type="submit" value="submit"/>
</form>
</body>
</html>

Register.java:
----------------------------------------
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class Register extends HttpServlet {
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

response.setContentType("text/html");
PrintWriter out = response.getWriter();
try{
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection connection=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","oracle");
PreparedStatement ps =connection.prepareStatement("insert into student values (?,?,?,?)");
ps.setInt(1,Integer.parseInt(request.getParameter("sid")));
ps.setString(2,request.getParameter("sname"));
ps.setString(3,request.getParameter("adrs"));
ps.setString(4,request.getParameter("phone"));
if(ps.executeUpdate()>0){
out.print("Registered Successfully");
}else{
out.print("Something Went Wrong!");
}
}catch (Exception e) {
e.printStackTrace();
}
out.flush();
out.close();
}
}

web.xml:
------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
<servlet-name>Register</servlet-name>
<servlet-class>Register</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>Register</servlet-name>
<url-pattern>/register</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>

Note: Don´t forget to include ojdbc14.jar

Posted On : 2016-08-13 00:03:07
Satisfied : 1 Yes  0 No
profile Rishi Kumar - anyforum.in Rishi Kumar
523185319357
Reply This Thread
up-rate
5
down-rate



Post Answer
Please Login First to Post Answer: Login login with facebook - anyforum.in