在Servlet上,当使用Post方法时,值为null,但是当使用Get方法时,则为罚款

时间:2017-07-10 16:30:55

标签: jsp servlets

我正在使用jsp页面插入一些值,但是当参数到达servelet时,我使用Post方法时收到的值为null,但是当使用Get方法时,它工作正常。
   request.getParameter()在使用post方法时返回null值但是当使用get方法时它工作正常并且值被插入

任何人都知道如何解决这个问题?

// Jsp Page

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <link rel="stylesheet" href="insert.css">
    <title>Database Update</title>
    </head>
    <body>
    <h1 class="main-header">Enter the Details</h1>
    <div class="fullbody">

    <form action="Insert" method="post" enctype="multipart/form-data">
    <div class="text">
    <b>Stuid:</b>
    <input type="text" placeholder="stuid" name="stuid" pattern=[0-9]{1,} title="can only have numeric value" required>
    <b>Student Name:</b>
    <input type="text" placeholder="student name" name="name" pattern="[A-Za-z ]{1,45}" title="can only have alphabet and space" required>
    <b>Student Mobile no.:</b>
    <input type="text" placeholder="student mobile no." name="stumob" pattern=[0-9]{10} title="10 digit number" required>
    <br>
    <b>Father's Name:</b>
    <input type="text" placeholder="Father's name" name="fname" pattern="[A-Za-z ]{1,45}" title="can only have alphabet and space" required>
    <b>Father's Mobile no.:</b>
    <input type="text" placeholder="father's mobile no." name="fmob" pattern=[0-9]{10} title="10 digit number" required>
    <br>
    <b>Mother's Name:</b>
    <input type="text" placeholder="Mother's name" name="mname" pattern="[A-Za-z ]{1,45}" title="can only have alphabet and space" required>
    <b>Gender:</b>
    <input type="radio" value="male" name="gender" required> Male
    <input type="radio" value="female" name="gender"> Female
    <b>Date of Birth:</b>
    <input type="date" placeholder="dd/mm/yyyy" name="dob" required>
    <b>Address:</b>
    <input type="text" placeholder="address" name="addr" required>
    <br>
    <b>Programme:</b>
    <select name="programme">
      <option value="B.tech">B.tech</option>
      <option value="M.tech">M.tech</option>
      <option value="BBA">BBA</option>
      <option value="BCA">BCA</option>
    </select>
    <b>Branch Code:</b>
    <input type="text" placeholder="branch code" name="branch" pattern=[A-Za-z]{3,6} title="can only have alphabet" required>
    <b>Year:</b>
    <select name="year">
      <option value="1st">1st</option>
      <option value="2nd">2nd</option>
      <option value="3rd">3rd</option>
      <option value="4th">4th</option>
    </select>
    <br>
    <b>Photo Upload:</b>
    <input type="file" name="photo" required>
    </div>
    <div class="submit">
    <input type="Submit" value="Submit">
    </div>
    </form>
    </div>
    </body>
    </html>


    //servlet

    package java_app1;

    import java.io.*;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;

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


    public class Insert extends HttpServlet {
        private static final long serialVersionUID = 1L;

        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            response.setContentType("text/html");
            String stuid=request.getParameter("stuid");
            String name=request.getParameter("name");
            String stumob=request.getParameter("stumob");
            String fname=request.getParameter("fname");
            String fmob=request.getParameter("fmob");
            String mname=request.getParameter("mname");
            String gender=request.getParameter("gender");
            String dob=request.getParameter("dob");
            String addr=request.getParameter("addr");
            String programme=request.getParameter("programme");
            String branch=request.getParameter("branch");
            String year=request.getParameter("year");
            String photo=request.getParameter("photo");       

            PrintWriter out=response.getWriter();
            out.println(stuid);

            try{
                   Class.forName("com.mysql.jdbc.Driver");
                   Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/student","root","ankit");               
                   PreparedStatement ps=con.prepareStatement("insert into student.stulog values (?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
                   ps.setString(1,stuid);
                   ps.setString(2,fmob);
                   ps.setString(3,name);
                   ps.setString(4,stumob);
                   ps.setString(5,fname);
                   ps.setString(6,fmob);
                   ps.setString(7,mname);
                   ps.setString(8,gender);
                   ps.setString(9,dob);
                   ps.setString(10,addr);
                   ps.setString(11,programme);
                   ps.setString(12,year);
                   ps.setString(13,branch);
                   ps.setString(14,photo);
                   int rs=ps.executeUpdate(); 
                   if(rs!=0)
                   {
                       out.println("[SUCCESS] RECORDS INSERTED SCUCCESSFULLY");
                       RequestDispatcher rd=request.getRequestDispatcher("/dashboard.jsp");  
                       rd.include(request, response); 
                   }
                   else
                   {
                       out.println("[FAILURE] RECORDS NOT INSERTED");
                       RequestDispatcher rd=request.getRequestDispatcher("/dashboard.jsp");  
                       rd.include(request, response); 
                   }
            }catch (Exception e)
              {
                out.println("Error:"+e);  
                e.printStackTrace();
              }
        }

    }

2 个答案:

答案 0 :(得分:0)

尝试这种方式对我来说很好用

在jsp中:

<form action="${pageContext.request.contextPath}/binod" method="post">
<input type="text" name="firstName"/>
<input type="submit"  value="login"/>
</form>

在servlet中:

@WebServlet(urlPatterns="/binod")
public class Test extends HttpServlet{

   @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("your name is"+req.getParameter("firstName"));

    }
}

您必须override

中的doPost HttpServlet方法

答案 1 :(得分:0)

步骤1: 导入

javax.servlet.annotation.MultipartConfig; 

步骤2: 添加注释

@MultipartConfig(maxFileSize = 16177215)    //Length of the file

在webServlet,urlPatterns

之后

就是这样!