此问题在此处已有答案:
How can I upload files to a server using JSP/Servlet?(14个答案)
27天前关闭。
我正在使用jsp页面插入一些值,但是当参数到达servelet时,当我使用Post方法时,收到的值为空,但是当使用Get方法时,它工作正常。
当使用post方法时,request.getParameter()返回空值,但当使用get方法时,它工作正常,值被插入
有人知道怎么解决这个问题吗?
//JSP页面
<%@ 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();
}
}
}
3条答案
按热度按时间llycmphe1#
试试这个方法对我很有效
在jsp中:
在servlet中:
您必须从
HttpServlet
override
doPost
方法eyh26e7m2#
步骤1:导入
步骤2:添加注解
在webServlet、urlPatterns之后
就是这样!
c86crjj03#
将注解添加到servlet
@MultipartConfig