如何将jsp文本框值输入到servlet

时间:2017-02-23 16:04:12

标签: java jsp servlets

我在该页面中有 Registration_Page.jsp 我有一个表单..我想使用servlet将表单提交到数据库。我的servlet名称是 Save_User_Details.java 我希望将jsp页面表单数据放入servlet ..它获取空值。如何将这些值输入到servlet中,任何人都可以帮助我,我是java的新手... 的 Registration_Page.jsp

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title> Registration Page</title>
<script language="JavaScript" src="gen_validatorv4.js"
    type="text/javascript" xml:space="preserve"></script>
</head>
<body>
    <h2 style="color: #ff6666" align="center">NEW USER REGISTRATION HERE</h2>
    <form ENCTYPE="multipart/form-data" action="Save_User_Details"
        method="POST" align="center" name="myform">
        <table border="02" align="center" style="color: #ff3399">


            <tr>
                <th style="color:#ff6666">FIRST NAME:</th>
                <td><input type="text" name="FName" id="FName" placeholder="First name" /></td>
            </tr>
            <tr>
                <th style="color:#ff6666">LAST NAME:</th>
                <td><input type="text" name="LName" id="LName" placeholder="Last name" /></td>
            </tr>

            <tr>
                <th style="color:#ff6666">PASSWORD:</th>
                <td><input type="password" name="password" id="password" placeholder="password"></td>
            </tr>

            <!-- <tr>
                <th>CONFORM PASSWORD:</th>
                <td><input type="password" name="conform_password" placeholder="conform password"></td>
            </tr> -->

            <tr>
                <th style="color:#ff6666">EMAIL ID:</th>
                <td><input type="email" name="mailid" id="mailid" placeholder="Mailid"></td>
            </tr>


            <tr>
                <th style="color:#ff6666">MOBILE NUMBER</th>
                <td><input type="number" name="mobile" id="mobile" placeholder="Mobile number"></td>
            </tr>




            <tr>
                <th style="color:#ff6666">GENDER:</th>
                <td style="color:#ff6666"><input type="radio" value="male" name="gender" id="gender"
                    checked="true">MALE <input type="radio" value="female"
                    name="gender">FEMALE <input type="radio" value="other"
                    name="gender">OTHER<br> <br></td>
            </tr>



            <tr>
                <th style="color:#ff6666"><label>DATE OF BIRTH:</label></th>
                <td><input type="date" name="dob" id="dob"><br> <br></td>
            </tr>

            <tr>
                <th style="color:#ff6666">COUNTRY:</th>
                <td><select name="Country" id="country" style="width: 150px">
                        <option value="000" selected="selected">[choose yours]</option>

                        <!-- <select name="country" style="width:150px"> -->
                        <option>India</option>
                        <option>USA</option>
                        <option>UK</option>
                        <option>Other</option>
                </select>



            <tr>
                <th></th>
                <td><input type="submit" style="color: #ff3399" value="SUMIT" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                    <button style="color: #ff3399" type="reset">CLEAR</button>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

                </td>
            </tr>
            </tr>
            <tr>
                <td></td>
                <td><a href="Login.jsp">LOGIN PAGE</a></td>
            </tr>

        </table>
    </form>

    <script language="JavaScript" type="text/javascript"
        xml:space="preserve">
        //<![CDATA[
        //You should create the validator only after the definition of the HTML form
        var frmvalidator = new Validator("myform");


        frmvalidator.addValidation("FName", "req",
                "Please enter your First Name");
        frmvalidator.addValidation("FName", "maxlen=20",
                "Max length for FirstName is 20");
        frmvalidator.addValidation("FName", "minlen=4",
                "Min length for FirstName is 4");
        frmvalidator.addValidation("FName", "alpha", "Alphabetic chars only");

        frmvalidator.addValidation("LName", "req",
                "Please enter your Last Name");
        frmvalidator.addValidation("LName", "maxlen=20", "Max length is 20");
        frmvalidator.addValidation("LName", "minlen=4",
                "Min length for LastName is 4");

        frmvalidator.addValidation("password", "maxlen=10");
        frmvalidator.addValidation("password", "req");
        frmvalidator.addValidation("password", "");
        frmvalidator.addValidation("password", "minlen=6",
                "Min length for Password is 6");





        frmvalidator.addValidation("mailid", "maxlen=50");
        frmvalidator.addValidation("mailid", "req");
        frmvalidator.addValidation("mailid", "email");
        frmvalidator.addValidation("mailid", "minlen=6",
                "Min length for Mailid is 6");

        frmvalidator.addValidation("gender", "dontselect=000");
        frmvalidator.addValidation("gender", "req");

        frmvalidator.addValidation("mobile", "maxlen=10");
        frmvalidator.addValidation("mobile", "numeric");
        frmvalidator.addValidation("mobile", "req");
        frmvalidator.addValidation("mobile", "minlen=10",
                "Mobile number should be 10 numbers");

        frmvalidator.addValidation("dob", "maxlen=40");
        frmvalidator.addValidation("dob", "req");

        frmvalidator.addValidation("Country", "dontselect=000");
        frmvalidator.addValidation("Country", "req");



        //]]>
    </script>
</body>
</html>

Save_User_Details.java

package com.ih.Control;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ih.Dao.UserDao;
import com.ih.Model.User_Details;

/**
 * Servlet implementation class Save_User_Details
 */
@WebServlet("/Save_User_Details")
public class Save_User_Details extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public Save_User_Details() {
        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
        response.getWriter().append("Served at: ").append(request.getContextPath());
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
        User_Details ud=new User_Details();
        System.out.println("i am in Save_User_Details..........");
        PrintWriter out=response.getWriter();
        int status=0;
        String fname=request.getParameter("FName");
        System.out.println("The FNAME::"+fname);
        String lname=request.getParameter("LName");
        System.out.println("The LNAME::"+lname);
        String password=request.getParameter("password");
        System.out.println("The PASSWORD::"+password);
        String mailid=request.getParameter("mailid");
        String mobile=request.getParameter("mobile");
        String gender=request.getParameter("gender");
        String dob=request.getParameter("dob");
        String country=request.getParameter("country");
        System.out.println("FNAME::"+fname+"LNAME::"+lname+"PASSWORD::"+password+"MAILID::"+mailid+"MOBILE::"+mobile+"GENDER::"+gender+"DOB::"+dob+"COUNTRY::"+country);
        ud.setFName(fname);
        ud.setLName(lname);
        ud.setPassword(password);
        ud.setMailid(mailid);
        ud.setMobile(mobile);
        ud.setGender(gender);
        ud.setDob(dob);
        ud.setCountry(country);
        try {
            UserDao.SaveRegistration_Details();
            RequestDispatcher rd=request.getRequestDispatcher("Login.jsp");
            rd.forward(request, response);
        } catch (SQLException e) {
            out.println("<h1 style='color:red'>FAILED TO INSERT USER DATA TRY AGAIN!!</h1>");
            out.println("<a href='Registration_Page.jsp'>REGISTER AGAIN</a>");
            e.printStackTrace();
        }



    }

}

0 个答案:

没有答案