在个人资料页面上提交表单后,它会转到控制器,然后它会更新数据库,然后重定向回到表单所在的同一页面。但是我在尝试返回同一页面时遇到错误。然而,我可以重定向到另一页没有任何问题? 任何帮助都会很棒。
表格
<form action="controller" method="post">
Link Name:<br>
<input type="text" name="createLinkName">
<br>
Link Content:<br>
<input type="text" name="createLinkContent">
<br>
<input type="hidden" name="command" value="createLink" />
<input type="submit" value="Submit">
</form>
抓取表单请求
else if (command.equals("createLink"))
{
String linkName = request.getParameter("createLinkName");
String linkContent = request.getParameter("createLinkContent");
User sessionU = new User();
sessionU = (User)request.getSession().getAttribute("user");
int userID = sessionU.getId();
System.out.println(userID);
createLink(request, response, linkName, linkContent, userID);
}
建立连结()
protected void createLink(HttpServletRequest request, HttpServletResponse response, String name, String content, int userID) throws IOException, ServletException
{
profileHandler ph = new profileHandler();
ph.createLink(request, response, name, content, userID);
request.getRequestDispatcher("profilePage.jsp").forward(request, response);
}
处理sql的处理程序
public class profileHandler {
private final DatabaseManager dbMgr;
public profileHandler()
{
this.dbMgr = new DatabaseManager();
}
public void createLink(HttpServletRequest request, HttpServletResponse response, String name, String content, int userID)
{
dbMgr.establishConnection();
String CREATE_LINK = "INSERT INTO linkedaccounts (linkname, linkaddress, userid) VALUES ('" + name + "', '" + content + "', " + userID + ")";
dbMgr.prepareStatement(CREATE_LINK);
dbMgr.executePreparedStatement();
dbMgr.releaseConnection();
}
sql没有问题,因为它更新了数据库就好了。只是不允许我留在与表格相同的页面上或只是重定向回来!
再次感谢。 编辑- 错误日志:
Warning: StandardWrapperValve[jsp]: Servlet.service() for servlet jsp threw exception
java.lang.NullPointerException
at org.apache.jsp.profilePage_jsp._jspService(profilePage_jsp.java:66)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:111)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:411)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:473)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:377)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1682)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:318)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:734)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:673)
at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:174)
at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:415)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:282)
at com.sun.enterprise.v3.services.impl.ContainerMapper$HttpHandlerCallable.call(ContainerMapper.java:459)
at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:167)
at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:201)
at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:175)
at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:235)
at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:284)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:201)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:133)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:112)
at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:561)
at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:112)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:117)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:56)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:137)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:565)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:545)
at java.lang.Thread.run(Thread.java:745)