从JSP中的request.getParameter()获取来自Servlet的Null值

时间:2017-06-29 11:25:14

标签: java jsp servlets

我正在从jsp向servlet发送文本值,但是在servlet中,request.getParameter()给出了空值。下面我提到我的JSP,Servlet和web.xml

REGISTER.JSP  
<form id="contactform"  action="servlet/SaveInfo" method="post"> 

    <p class="contact"><label for="email">Email</label></p> 
    <input id="email" name="email" placeholder="example@domain.com" required="" tabindex="1" type="email">

    <p class="contact"><label for="username">User Name</label></p> 
    <input id="username" name="username" placeholder="username" required="" tabindex="2" type="text"> 

    <p class="contact"><label for="password">Password</label></p> 
    <input type="password" id="password" name="password" required="" tabindex="3"> 

    <p class="contact"><label for="repassword">Confirm  Password</label></p> 
    <input type="password" id="repassword" name="repassword" required=""> 

    <p class="contact"><label for="phone">Mobile Phone</label></p> 
             <input id="phone" name="phone" placeholder="phone number" required="" tabindex="4" type="text"> <br>

    <p class="contact"><label for="name">Organization Name</label></p> 
            <input id="Orgname" name="name" placeholder="Organization name" required="" tabindex="5" type="text"> 

    <p class="contact"><label for="Address">Address</label></p> 
            <input id="address" name="name" placeholder="Street,Area,city" required="" tabindex="6" type="text"> <br>


    <!--     <select class="select-style" name="BirthMonth"> -->
            <label>State</label><br>
             <select  class="select-style" name="BirthMonth">
              <%
                        ResultSet rs = null;
                                Connection con = null;
                                            PreparedStatement pmst1= null;
                                           int countryid = 208;
                                           try{

                                              con = DBConnectivity.getOracleDBConnection();
                                              pmst1 = con.prepareStatement(Sqlquery.getRegion());

                                             pmst1.setInt(1, countryid);
                                             rs=pmst1.executeQuery();

                                           while(rs.next()){
                                               String name = rs.getString("name"); 
                                               %>
                                              <option value="<%=name %>"><%=name %></option>

                                         <%   }   %>     

                                           <%
                                          }catch(Exception e){
                                         }                                                     

                                %>

           </select><br><br>



        <p class="contact"> <input id="check" name="name" style="margin-right: 10px" type="checkbox">
        <label for="name"> Not Yet Registered GSTIN</label> </p>


        <p class= "GSTIN"><label for="GSTIN">GSTIN No.</label></p><br>
        <input id = "GSTINNO" maxlength="15"  name= "GSTIN" placeholder="GSTIN No." type ="text"><br><br>

        <input class="buttom" name="submit" id="submit" tabindex="5" value="Sign me up!" type="submit">     

SaveInfo.java
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {          
     try{
             ResultSet rs = null;
             Connection con = null;
             PreparedStatement pmst1= null;
             String Orgname =  request.getParameter("Orgname");  
             String check =request.getParameter("check");
             String GSTINNO =  request.getParameter("GSTINNO");
             con = DBConnectivity.getOracleDBConnection();
             int   clientid = 1000014;
             MClient Client = new MClient (Env.getCtx(), clientid, null);

我想要我的servlet类中的所有值并将这些值保存在数据库中,我提到了我的web.xml文件 请帮助我

<servlet>
<description></description>
<display-name>SaveInfo</display-name>  
<servlet-name>SaveInfo</servlet-name>  
<servlet-class>com.org.register.SaveInfo</servlet-class>  

<servlet-mapping>  
    <servlet-name>SaveInfo</servlet-name>  
    <url-pattern>/servlet/SaveInfo</url-pattern>  
</servlet-mapping>

3 个答案:

答案 0 :(得分:1)

在Servlet中,您需要按name属性获取参数,而不是id

例如,您有以下内容,

<input id="Orgname" name="name" placeholder="Organization name" required="" tabindex="5" type="text"> 

您已将id设为Orgnamename参数为name。所以在Servlet中你会这样做,

request.getParameter("name");

但你在做,

String Orgname =  request.getParameter("Orgname");

其次,两个参数的名称不能相同。对于以下两种情况,您将名称命名为

<input id="Orgname" name="name" placeholder="Organization name" required="" tabindex="5" type="text"> 

<input id="address" name="name" placeholder="Street,Area,city" required="" tabindex="6" type="text"> <br>

为参数指定一些不同的名称。

答案 1 :(得分:0)

JSP post参数使用其属性名称。你在3个参数中写了错误的属性名称。

变化:

<input id="check" name="check"
<input id="Orgname" name="Orgname"
<input id="address" name="address"

要:

library("boot") 
# This example is taken from Exercise 7.5 of Gill, Murray and Wright (1991).
enj <- c(200, 6000, 3000, -200)
fat <- c(800, 6000, 1000, 400)
vitx <- c(50, 3, 150, 100)
vity <- c(10, 10, 75, 100)
vitz <- c(150, 35, 75, 5)
j<-simplex(a = enj, A1 = fat, b1 = 13800, A2 = rbind(vitx, vity, vitz),
b2 = c(600, 300, 550), maxi = TRUE)
xx<-(j["soln"])
print(xx)

答案 2 :(得分:0)

请求对象的getParameter函数使用name属性。所以请用以下内容替换你的jsp。

<form id="contactform"  action="servlet/SaveInfo" method="post"> 

        <p class="contact"><label for="email">Email</label></p> 
        <input id="email" name="email" placeholder="example@domain.com" required="" tabindex="1" type="email">

        <p class="contact"><label for="username">User Name</label></p> 
        <input id="username" name="username" placeholder="username" required="" tabindex="2" type="text"> 

          <p class="contact"><label for="password">Password</label></p> 
        <input type="password" id="password" name="password" required="" tabindex="3"> 

         <p class="contact"><label for="repassword">Confirm  Password</label></p> 
        <input type="password" id="repassword" name="repassword" required=""> 

        <p class="contact"><label for="phone">Mobile Phone</label></p> 
         <input id="phone" name="phone" placeholder="phone number" required="" tabindex="4" type="text"> <br>

        <p class="contact"><label for="name">Organization Name</label></p> 
        <input id="Orgname" name="Orgname" placeholder="Organization name" required="" tabindex="5" type="text"> 

         <p class="contact"><label for="Address">Address</label></p> 
        <input id="address" name="address" placeholder="Street,Area,city" required="" tabindex="6" type="text"> <br>


    <!--     <select class="select-style" name="BirthMonth"> -->
        <label>State</label><br>
         <select  class="select-style" name="BirthMonth">
          <%
                    ResultSet rs = null;
                            Connection con = null;
                                        PreparedStatement pmst1= null;
                                       int countryid = 208;
                                       try{

                                          con = DBConnectivity.getOracleDBConnection();
                                          pmst1 = con.prepareStatement(Sqlquery.getRegion());

                                         pmst1.setInt(1, countryid);
                                         rs=pmst1.executeQuery();

                                       while(rs.next()){
                                           String name = rs.getString("name"); 
                                           %>
                                          <option value="<%=name %>"><%=name %></option>

                                     <%   }   %>     

                                       <%
                                      }catch(Exception e){
                                     }                                                     

                            %>

       </select><br><br>



    <p class="contact"> <input id="check" name="name" style="margin-right: 10px" type="checkbox">
    <label for="name"> Not Yet Registered GSTIN</label> </p>


    <p class= "GSTIN"><label for="GSTIN">GSTIN No.</label></p><br>
    <input id = "GSTINNO" maxlength="15"  name= "GSTIN" placeholder="GSTIN No." type ="text"><br><br>

    <input class="buttom" name="submit" id="submit" tabindex="5" value="Sign me up!" type="submit">     
相关问题