日期:2014-05-17  浏览次数:20785 次

再次求助:servlet执行完后无法实现跳转
首先是一个mainpage.jsp页面有个form填写数据后提交,action就是servlet。点提交后可以成功插入数据库,但是不能返回,试过用了response.sendRedirect("mainpage.jsp");和getRequestDispatcher也不行,想请教一下高手,我是初学者,帮我看一下代码,请见谅!
Java code
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws IOException 
    {

        try 
        {
            PrintWriter out = response.getWriter();
            response.setContentType("text/html;charset=gb2312");
            out.println("<HTML>");
            out.println("<BODY>");
            out.println("<a href='mainpage.jsp'>返回</a>");
            out.println("</BODY>");
            out.println("</HTML>");
            DBBean dbbean = new DBBean();
            request.setCharacterEncoding("utf-8");
            String insert = "insert into news(num,theme,author,date) values('"
                    + request.getParameter("num") + "','"
                    + request.getParameter("theme") + "','"
                    + request.getParameter("author") + "','"
                    + request.getParameter("date") + "')";
            if (null != request.getParameter("num")&& !"".equals(request.getParameter("num"))) 
            {
                dbbean.update(insert);
            }
        } 
            catch (Exception ex) 
            {
            ex.printStackTrace();
            }
            finally 
            {
            DBBean dbbean = new DBBean();
            dbbean.close();
            }
     
      }
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doPost(request, response);
    }
}


对了,点击返回后是servlet/mainpage.jsp。 错误404。还有就是 返回 两个字 是乱码。 高手们帮帮我啦。

这是 mainpage的form代码
Java code
<form onSubmit="return validate()"  method="post" name="biaoge" action="servlet/MyServlet" ><br>
编号:<input type="text" name="num" ><br>
<br>
主题:<input type="text" name="theme" ><br>
<br>
作者:<input type="text" name="author" ><br>
<br>
日期:<input type="text" name="date"  ><br>
<br>
<input type="submit" value=提交></form>


------解决方案--------------------
response.sendRedirect("/mainpage.jsp");

少了斜杠。