如何使用ajax在同一servlet中实现doPost()和doGet()请求?

时间:2018-08-12 17:27:30

标签: javascript java ajax jsp servlets

我正在jsp servlet上做一个小项目,我想举一个示例来使用相同的servlet来实现doGet()和doPost()方法,并通过asp调用以jsp页面的形式发送和发布。

还指定为什么不建议在同一个servlet中使用doGet()和doPost()的原因。如何在同一个jsp页面中的两个不同servlet中实现相同的实现。

在此先感谢您的帮助。

我的jsp代码:-

 <form action="/mamababu.do" method="POST">
    <select name="command_no">
          <c:forEach var="items" items="${scriptItems}">

            <option value="${items.command}" name="command">${items.command}</option>

          </c:forEach>
       </select>
       <input type="submit" value="submit"></input>
</form> 

我的servlet类:-

package com.project.mamabhagne;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;


@WebServlet("/mamababu.do")
public class mamababu extends HttpServlet {

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        //get the data from database ie the model class
        try {
            List<Script> scriptitems=modelDBUtil.getScriptList();
           // String json = new Gson().toJson(scriptitems);

            request.setAttribute("scriptItems", scriptitems);
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        /*String itemsfood[]={"biriyani","rice"};
        request.setAttribute("itemsfood",itemsfood)*/;



        //redirect to a different page
        RequestDispatcher dispatcher =request.getRequestDispatcher("scriptviewer.jsp");

        dispatcher.forward(request, response); 
    }
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //TODO Auto-generated method stub
        System.out.println("hi");
         response.setContentType("text/html");
              PrintWriter out = response.getWriter();
              out.println("<HTML><HEAD><TITLE>Hello World!</TITLE>"+
              "</HEAD><BODY>Hello World!</BODY></HTML>");
              out.close();
              RequestDispatcher dispatcher =request.getRequestDispatcher("scriptviewer.jsp");
            dispatcher.forward(request, response); 
    }


}

发布表单数据时出现此错误:-

HTTP Status 404 - /mamababu.do


type Status report

message /mamababu.do

description The requested resource is not available.


Apache Tomcat/8.0.52

1 个答案:

答案 0 :(得分:0)

我不清楚您的问题,顺便说一句,据我所知,您需要一个servlet并同时响应GET和POST请求(通过AJAX)。

@WebServlet(name = "MyServelet", urlPatterns = {"/myservletForm"})
public class MyServlet extends HttpServlet {
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response)
        throws IOException {
    response.getWriter().println("Hello This is GET Response.");
  }
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws IOException{
response.getWriter().println("Hello This is POST Response.");
}
}
如上,当您发送“获取”请求时,

转到doGet而“发布”请求转到doPost。您可以通过声明特定的Ajax请求类型来选择需要调用GET或POST。