所以基本上,当我尝试提交jsp时,控件转到了servlet,但是jsp中所有控件的值都变为空
“ ViewBook.jsp”
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@include file="Header_1.jsp" %>
<form action="<%=request.getContextPath()%>/BookServlet" method="post"
enctype="multipart/form-data">
<table>
<th colspan="2"></th>
<tr>
<td>
ID:
</td>
<td>
<input type="number" name="id">
</td>
</tr>
<tr>
<td>
Title:
</td>
<td>
<input type="text" name="title">
</td>
</tr>
<tr>
<td>
Author
</td>
<td>
<input type="text" name="author">
</td>
</tr>
<tr>
<td>
Price:
</td>
<td>
<input type="number" name="price">
</td>
</tr>
<tr>
<td>
Book image:
</td>
<td>
<input type="file" name="book_img">
</td>
</tr>
<tr>
<td>
Publish date:
</td>
<td>
<input type="date" name="pub_date">
</td>
</tr>
<tr>
<td>
<input type="submit" value="add" formaction="
<%=request.getContextPath()%>/BookServlet?action=add">
<input type="submit" value="delete"
formaction="G:\sem5\AJT\Q-4\src\java\BookServle?action=delete">
<input type="submit" value="update" formaction="
<%=request.getContextPath()%>/BookServlet?action=update">
<input type="submit" value="view" formaction="
<%=request.getContextPath()%>/BookServlet?action=view">
</td>
</tr>
</table>
</form>
<%@include file="Footer.jsp" %>
“ BookServlet.java”
@WebServlet (name="BookServlet", urlPatterns = {"/BookServlet"})
public class BookServlet extends HttpServlet {
protected void processRequest(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse
response)
throws ServletException, IOException {
try (PrintWriter out = response.getWriter()) {
}
}
// processRequest(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse
response)
throws ServletException, IOException {
String title=request.getParameter("title");
String author=request.getParameter("author");
String price=request.getParameter("price");
System.out.println("price=="+price);
String book_img=request.getParameter("book_img");
System.out.println("book:imagre===="+book_img);
String pub_date=request.getParameter("pub_date");
String action=request.getParameter("action");
SimpleDateFormat sdf=new SimpleDateFormat("mm/dd/yyyy");
Date d = null;
try {
d = (Date) sdf.parse(pub_date);
} catch (Exception ex) {
System.out.println("Exception ex="+ex);
}
Part filepart=request.getPart("book_img");
System.out.println("part====="+filepart);
InputStream is=filepart.getInputStream();
BookBean bb=new BookBean();
// ResultSet rs=bb.ViewAllBooks();
if(action.equals("add"))
{
Book b=new Book(0,title,author,Integer.parseInt(price),d,is);
bb.add(b);
HttpSession s=request.getSession(true);
// s.setAttribute("rs", rs);
RequestDispatcher
rd=request.getRequestDispatcher("ViewAllBooks.jsp");
rd.forward(request, response);
// processRequest(request, response);
}else if(action.equals("update"))
{ Book b=new
Book(0,title,author,Integer.parseInt(price),d,is);
// boolean flag= bb.update(b);
HttpSession s=request.getSession(true);
// s.setAttribute("rs", rs);
RequestDispatcher
rd=request.getRequestDispatcher("ViewAllBooks.jsp");
rd.forward(request, response);
}else if(action.equals("delete")){}
else if(action.equals("view")){}
}
@Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>
}
这就是我抓到的东西 -Exception ex = java.lang.NullPointerException
StackTrace:
java.lang.NullPointerException
at BookCrud.BookServlet.doPost(BookServlet.java:136)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:660)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter
(ApplicationFilterChain.java:231)
at org.apache.catalina.core.ApplicationFilterChain.doFilter
(ApplicationFilterChain.java:166)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter
(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter
(ApplicationFilterChain.java:166)
at org.apache.catalina.core.StandardWrapperValve.invoke
(StandardWrapperValve.java:199)
at org.apache.catalina.core.StandardContextValve.invoke
(StandardContextValve.java:96)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke
(AuthenticatorBase.java:491)
at org.apache.catalina.core.StandardHostValve.invoke
(StandardHostValve.java:139)
at
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke
(AbstractAccessLogValve.java:668)
at
org.apache.catalina.core.StandardEngineValve.invoke
(StandardEngineValve.java:87)
at org.apache.catalina.connector.CoyoteAdapter.service
(CoyoteAdapter.java:343)
at
org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:408)
at org.apache.coyote.AbstractProcessorLight.process
(AbstractProcessorLight.java:66)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process
(AbstractProtocol.java:764)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun
(NioEndpoint.java:1388)
at
org.apache.tomcat.util.net.SocketProcessorBase.run
(SocketProcessorBase.java:49)
at java.util.concurrent.ThreadPoolExecutor.runWorker
(ThreadPoolExecutor.java:1149)
at
java.util.concurrent.ThreadPoolExecutor$Worker.run
(ThreadPoolExecutor.java:624)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run
(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:748)
您身边的任何帮助 另外,我在StackOverflow和其他网站上对此进行了很多冲浪,但是 我没有得到适当的解决方案。……
答案 0 :(得分:0)
您无法使用request.getParameter(name);
直接获取参数。在使用表单字段时,表单字段不能作为parameter
中的request
使用,它们包含在stream
中,因此您无法以正常方式获取它。
请参阅:http://commons.apache.org/proper/commons-fileupload//using.html,在“处理上载的项目”部分。
答案 1 :(得分:0)
对于所有面临相同错误的人::您可以使用@secret super star提到的jar文件,但是如果您不想这样做,这里有个提示:
只是
import javax.servlet.annotation.MultipartConfig;
在您的servlet中并添加
@MultipartConfig(maxFileSize = 16177215) //Length of the file
注释,就是这样
注意:在我的情况下,以下将是我的servlet
@WebServlet (name="BookServlet", urlPatterns = {"/BookServlet"})
@MultipartConfig(maxFileSize = 16177215)
public class BookServlet extends HttpServlet {
protected void processRequest(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse
response)
throws ServletException, IOException {
try (PrintWriter out = response.getWriter()) {
}
}
// processRequest(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse
response)
throws ServletException, IOException {
String title=request.getParameter("title");
String author=request.getParameter("author");
String price=request.getParameter("price");
System.out.println("price=="+price);
String book_img=request.getParameter("book_img");
System.out.println("book:imagre===="+book_img);
String pub_date=request.getParameter("pub_date");
String action=request.getParameter("action");
SimpleDateFormat sdf=new SimpleDateFormat("mm/dd/yyyy");
Date d = null;
try {
d = (Date) sdf.parse(pub_date);
} catch (Exception ex) {
System.out.println("Exception ex="+ex);
}
Part filepart=request.getPart("book_img");
System.out.println("part====="+filepart);
InputStream is=filepart.getInputStream();
BookBean bb=new BookBean();
// ResultSet rs=bb.ViewAllBooks();
if(action.equals("add"))
{
Book b=new Book(0,title,author,Integer.parseInt(price),d,is);
bb.add(b);
HttpSession s=request.getSession(true);
// s.setAttribute("rs", rs);
RequestDispatcher
rd=request.getRequestDispatcher("ViewAllBooks.jsp");
rd.forward(request, response);
// processRequest(request, response);
}else if(action.equals("update"))
{ Book b=new
Book(0,title,author,Integer.parseInt(price),d,is);
// boolean flag= bb.update(b);
HttpSession s=request.getSession(true);
// s.setAttribute("rs", rs);
RequestDispatcher
rd=request.getRequestDispatcher("ViewAllBooks.jsp");
rd.forward(request, response);
}else if(action.equals("delete")){}
else if(action.equals("view")){}
}
@Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>
}