我这几个小时一直在努力。
我正试图发一条推文(twit)
上下文:
我在这个webapp上有多个servlet(应该是Twitter的极简主义版本)。
web.xml的片段如下所示:
<welcome-file-list>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>membershipServlet</servlet-name>
<servlet-class>controller.membershipServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>twitServlet</servlet-name>
<servlet-class>controller.twitServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>membershipServlet</servlet-name>
<url-pattern>/membership</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>twitServlet</servlet-name>
<url-pattern>/twitpost</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
</web-app>
作业说要做一个推文的doPost。
应该控制它的servlet片段如下所示:
24 @WebServlet(name = "twitServlet", urlPatterns = {"/twitsubmit"})
25 public class twitServlet extends membershipServlet {
26
27 /**
28 * Handles the HTTP <code>POST</code> method.
29 *
30 * @param request servlet request
31 * @param response servlet response
32 * @throws ServletException if a servlet-specific error occurs
33 * @throws IOException if an I/O error occurs
34 */
35 @Override
36 protected void doPost(HttpServletRequest request, HttpServletResponse
response)
37 throws ServletException, IOException, NullPointerException {
38
39 String action = request.getParameter("action");
40 String url = "/home.jsp"; // Main start page //
41 HttpSession session = request.getSession();
42
43 if (action.equals("twitpost")) {
44 //get paramerterws
45 String twittext = request.getParameter("twitSubmit");
46
47 //store in the twit object
48 Twit mytwit = new Twit();
49 mytwit.setTwitText(twittext);
50
51
52 session.setAttribute("enteredTwit", twittext);
53 TwitDB.twit(twittext);
54 request.setAttribute("twit", twittext);
55 url = "/home.jsp";
56 session.setAttribute("twit", mytwit);
57 session.removeAttribute("enteredTwit");
58 }
59
60
61 getServletContext()
62 .getRequestDispatcher(url)
63 .forward(request, response);
64 }
尝试向数据库输入推文最终会出现500错误:
HTTP Status 500 – Internal Server Error
Type Exception Report
Description The server encountered an unexpected condition that prevented it
from fulfilling the request.
Exception
java.lang.NullPointerException
controller.twitServlet.doPost(twitServlet.java:44)
javax.servlet.http.HttpServlet.service(HttpServlet.java:660)
javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
如果有人能提供帮助,我将永远感激不尽。我一直在努力比这更长时间。
谢谢。