| | |
how to pass the parameters of jsp from one servlet to another servlet
Please support our JSP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
•
•
Join Date: Nov 2008
Posts: 28
Reputation:
Solved Threads: 0
Hi i created one jsp form with 2 fields..now in the first servlet i want check whether those 2 fields values are presented in the Database or not..if not then i want move those values to another servlet .. how can i do this?how to get the values (parameter values of jsp in second servlet.. .ie; in first servlet those values will be comapred with database and bcoz of send redirect in else the values will be stored if they not present in DB here is my code plz check it once..
1st Servlet..
_____________
2nd servlet
------------------
1st Servlet..
_____________
JSP Syntax (Toggle Plain Text)
package com; import java.io.*; import java.util.*; import java.sql.*; import javax.servlet.*; import javax.servlet.http.*; public class SearchServlet extends HttpServlet { private ServletConfig config; public void init(ServletConfig config) throws ServletException{ this.config=config; } public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException,IOException{ PrintWriter out = response.getWriter(); String connectionURL = "jdbc:mysql://localhost/test"; Connection connection=null; ResultSet rs; String mobileno=request.getParameter("mno"); String password=request.getParameter("pwd"); response.setContentType("text/html"); HttpSession ses=request.getSession(true); try { // Load the database driver Class.forName("com.mysql.jdbc.Driver"); // Get a Connection to the database connection = DriverManager.getConnection(connectionURL, "root", "password"); //Add the data into the database String sql = "select mobileno,password from newuser where mobileno='"+ mobileno + "'and password='"+ password + "' " ; Statement s = connection.createStatement(); rs=s.executeQuery (sql); rs = s.getResultSet(); if(rs.next ()) { mobileno=rs.getString("mobileno"); password=rs.getString("password"); out.println("MOBILENO=" +mobileno ); out.println("PASSWORD=" +password ); if(mobileno.equals(request.getParameter("mno")) && password.equals(request.getParameter("pwd"))) { out.print("<h1>You are Already Registerd </h1>"); } } else { request.setAttribute("MOBILENO",mobileno); request.setAttribute("PASSWORD",password); RequestDispatcher dispatcher = getServletConfig ( ) .getServletContext ().getRequestDispatcher("/LoginServlet"); dispatcher.forward (request, response) ; } rs.close (); s.close (); } catch(Exception e) { System.out.println("Exception is ;"+e); } } }
2nd servlet
------------------
JSP Syntax (Toggle Plain Text)
package com; import java.io.*; import java.util.*; import java.sql.*; import javax.servlet.*; import javax.servlet.http.*; public class LoginServlet extends HttpServlet{ private ServletConfig config; public void init(ServletConfig config) throws ServletException{ this.config=config; } public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException,IOException{ PrintWriter out = response.getWriter(); String connectionURL = "jdbc:mysql://localhost/test"; Connection connection=null; String mno=null; String pwd=null; String mobileno = (String)request.getAttribute(MOBILENO); String password = request.getAttribute(PASSWORD); response.setContentType("text/html"); try { // Load the database driver Class.forName("com.mysql.jdbc.Driver"); // Get a Connection to the database connection = DriverManager.getConnection(connectionURL, "root", "password"); //Add the data into the database String sql = "insert into newuser values(" + mobileno + ",'" + password + "')"; Statement s = connection.createStatement(); HttpSession ses=request.getSession(); int x = s.executeUpdate(sql); if(x==1) { out.println("<h1> Thanks For the Registration</h1>"); out.println("<br><h1> Welcome to 3P Technologies</h1></br>"); } else{ out.println("<h1>+Your Registration is failed..Plase try again+</h1>"); out.println("<a href='newregister.jsp'><br>Register Here!!</a>"); } s.close(); } catch(Exception e){ System.out.println("Exception is ;"+e); } } }
Last edited by peter_budo; Nov 24th, 2008 at 6:42 pm. Reason: Removing the [inlinecode] tags
So where are you encountering a problem ??
We are not Psychics to know what output you are getting on your computer screen.
Tell us the problem in clear words and we will see what is going wrong.
Also in the first servlet:-
why do you simply want to get the mobile no. and password from the database when you know that the mobile no. and password that you have is correct (cause it has already been validated by your SQL query).
We are not Psychics to know what output you are getting on your computer screen.
Tell us the problem in clear words and we will see what is going wrong.
Also in the first servlet:-
java Syntax (Toggle Plain Text)
if(rs.next()) { mobileno=rs.getString("mobileno"); password=rs.getString("password");
Last edited by stephen84s; Nov 24th, 2008 at 9:55 am.
"Any fool can write code that a computer can understand. Good programmers write code that humans can understand."
"How to ask questions the smart way ?"
"How to ask questions the smart way ?"
•
•
•
•
Hi i created one jsp form with 2 fields..now in the first servlet i want check whether those 2 fields values are presented in the Database or not..if not then i want move those values to another servlet .. how can i do this?how to get the values (parameter values of jsp in second servlet.. .ie; in first servlet those values will be comapred with database and bcoz of send redirect in else the values will be stored if they not present in DB here is my code plz check it once..
IMO, what you require are multiple request handlers instead of multiple servlets. Also, you entire business logic is placed in the servlet which is pretty bad. Create two classes called LoginController and SearchController which would be instantiated by the servlet or the request handler.
You are actually spitting out HTML in your servlet which was pretty cool a few years back, but now isn't. Use JSP along with JSTL for presentation purposes. The J2EE 5 guide is a free download. Read and be enlightened.
The romantic image of an über-programmer is someone who fires up Emacs, types like a machine gun, and delivers a flawless final product from scratch. A more accurate image would be someone who stares quietly into space for a few minutes and then says “Hmm. I think I’ve seen something like this before.” - John D
![]() |
Similar Threads
- Passing parameters back to calling document (JSP)
- Difference between <%@include%> and <jsp:include> (JSP)
- generating dynamic/runtime query (JSP)
Other Threads in the JSP Forum
- Previous Thread: Jstl number formatting
- Next Thread: Book about Struts2
Views: 3557 | Replies: 2
| Thread Tools | Search this Thread |
Tag cloud for JSP
apache array backbutton combobox comma connection csv database development directorystructure dropdownlist dynamicpagetitles eclipse frames glassfish ie8 imagetodatabse imageupload integer internet java javaee javascript jsf jsp jsppagetitles levels mvc2 mvcmodel2 mysql netbeans network parameters passing ping printinserverinsteadofclient project read redirect request.getparameter response seperated servlet servletdopost()readxml sessions software sql ssl state_saving_method stocks sun tomcat tutorial update values video web write






