0

I've a little doubt with this...how can i set/retrieve the value from a jsp page jscript string named "z" in a servlet.I need to use it in servlet...I'M exploring new thing n its a new thing for me as i"m new to these thing...Thanks for the quick help....i need the value of password if pass1 and pass2 are same,n then i need to retrieve it in servlet if pass1==pass2...tell me a way...for that i wrote a jscript to check pass1==pass2..

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>New User Registration</title>
    <script>
    function myFunction(){
        var x = document.forms["newForm"]["pass1"].value;
        var y = document.forms["newForm"]["pass2"].value;
    if(x==y){
        document.newForm.submit();var z=x;
        return true;
        }
        else {
        alert("Passwords not matching!!!");}
        }

    </script>
    </head>
    <body>
    <h1>Form</h1>
    <fieldset>
    <form name=newForm action="RegServlet">Username:<input
        type="text" name="username"><br>
    Password:<input type="text" name="pass1" id="pass1"><br>
    Confirm Password:<input type="text" name="pass2" id="pass2"><br>
    <input type="submit" onclick=myFunction() value="Create"></input></form>
    </fieldset>

    </body>
    </html>

servlet

package myPack;

import java.io.IOException;
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;

/**
 * Servlet implementation class RegServlet
 */
public class RegServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public RegServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        String s1=request.getParameter("username");
        System.out.println(s1);

        String s2=request.getParameter("");//HERE I NEED THE PAssword value if PASS!==PASS2
        System.out.println(s2);

        String c="jdbc:mysql://localhost:3306/test";

        Connection con=null;
        System.out.println("Connection OK");
        try{
        Class.forName("com.mysql.jdbc.Driver").newInstance();
            System.out.println("Done2");

            con = DriverManager.getConnection(c, "root", "MyNewPass");
            System.out.println("Done3");

            PreparedStatement ps=null;
            System.out.println("Done4");

            String qs = "insert into userinfo values(?,?);";
            ps = con.prepareStatement(qs);
            ps.setString(1,s1);
            ps.setString(2,s2);
            System.out.println("Success");
            ps.execute();
            con.close();

        }
        catch (Exception e) {
            System.out.println("Failed: " + e.toString());
        // TODO: handle exception
            System.out.println("Failed");}}


    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
    }

}

2 Answers 2

1

Create a hidden field in your form; then in your "onsubmit" event set the value of that field to z.

 <input type="hidden" name="zValue" id="zValue">

in onsubmit event

document.getElementById("zValue").value="The value I want to send";

and retrieve in your servlet as any other parameter.

Sign up to request clarification or add additional context in comments.

3 Comments

Bro i already set the value of z in the above jsp as var z=x;......what i need is to retrieve the value of z in a servlet....
The servlet will only receive the data from the input/select/textArea fields in the form that you submit. All of the JS variables will be ignored.
Hm!! Thnx Bro i need the value of password if pass1 and pass2 are same,n then i need to retrieve it in servlet if pass1==pass2...tell me a way...for that i wrote a jscript to check pass1==pass2
0

The following is an example i use in tomcat. It will get you all parameters that are send in a POST or GET request. Be advised that this does not cover multicast requests (which are needed for file transfers).
I don't know if it will work for you, as you have not specified you servlet container.

import java.io.*;
import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.util.*;


@WebServlet(description = "A simple request test.", urlPatterns = { "/requesttest" })

public class RequestTest extends HttpServlet {

public void doGet(HttpServletRequest request,
                    HttpServletResponse response)
      throws ServletException, IOException {
    response.setContentType("text/html");
    PrintWriter out = response.getWriter();
    String title = "Reading All Request Parameters";
    out.println("<BODY BGCOLOR=\"#FDF5E6\">\n" +
                "<H1 ALIGN=CENTER>" + title + "</H1>\n" +
                "<TABLE BORDER=1 ALIGN=CENTER>\n" +
                "<TR BGCOLOR=\"#FFAD00\">\n" +
                "<TH>Parameter Name<TH>Parameter Value(s)");
    Enumeration<String> paramNames = request.getParameterNames();
    while(paramNames.hasMoreElements()) {
      String paramName = (String)paramNames.nextElement();
      out.println("<TR><TD>" + paramName + "\n<TD>");
      String[] paramValues = request.getParameterValues(paramName);
      if (paramValues.length == 1) {
        String paramValue = paramValues[0];
        if (paramValue.length() == 0)
          out.print("<I>No Value</I>");
        else
          out.print(paramValue);
      } else {
        out.println("<UL>");
        for(int i=0; i<paramValues.length; i++) {
          out.println("<LI>" + paramValues[i]);
        }
        out.println("</UL>");
      }
    }
    out.println("</TABLE>\n</BODY></HTML>");

  }

  public void doPost(HttpServletRequest request,
                     HttpServletResponse response)
      throws ServletException, IOException {
    doGet(request, response);
  }
}

EDIT
Seeing as you edited your question with your servlet code, the answer should be really simple.

 String s2=request.getParameter("pass1");

This should get you the value that is transmitted within the password field. This is no different than you getting the username with String s1=request.getParameter("username");

1 Comment

i've posted the servlet...thnx for the help ;-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.