【问题标题】:Jsp request is not going to java servlet classJsp请求不会去java servlet类
【发布时间】:2015-11-17 05:38:02
【问题描述】:

我有一个表,其中包含带有查看、更新、删除图标的记录。 在单击查看和更新​​时,我将我的请求传递给其他一些 jsp。单击删除图标,请求需要进入我的 servlet 类。它应该在那里执行删除查询。

这是我的桌子

单击删除图标后,我将在 url 中获取 id 和删除值。看图片。但是请求不会发送到 java servlet。

这是我的代码

jsp代码:

<%
    FormRegistration fr = new FormRegistration();
    List<UserDetailsBean> list = fr.getUserDetails();
   %>
<form method="post" action="./BrightSymphonyController" >
 <%
    for(int i=0;i<list.size();i++){
  %>
    <table id="example" class="display" cellspacing="0" width="100%">
      <thead>
         <tr>
           <th style="display: none;">ID</th>
         </tr>
      </thead>
      <tbody>
        <td>
          <a href="./BrightSymphonyController?id=<%=list.get(i).getId()%>&delete=Delete" class="urlImgDelete" title="Delete"><!--here i have delete icon -->
          </a>
        </td>
    <%}%>
    </tbody>
    </table>
</form>

Servlet 类:

@WebServlet("/BrightSymphonyController")
public class BrightSymphonyController extends HttpServlet {
/**
 * @see HttpServlet#HttpServlet()
 */
public BrightSymphonyController() {
    //super();
    // TODO Auto-generated constructor stub
}
/**
 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse       response)
 */
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
}
/**
 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
 */
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String deleteuser = request.getParameter("delete");
        System.out.println("deleteuser============"+deleteuser);
        String id=request.getParameter("id");
        System.out.println("id============"+id);
        }
}

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
 <display-name>brightsymphony</display-name>
 <welcome-file-list>
 <welcome-file>index.jsp</welcome-file>
 </welcome-file-list>
 <servlet>
<servlet-name>BrightSymphony</servlet-name>
<servle-class>com.s2s.brightsymphony.controller.BrightSymphonyController
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>BrightSymphony</servlet-name>
<url-pattern>/BrightSymphony</url-pattern>
</servlet-mapping>
</web-app>

请帮帮我。

【问题讨论】:

    标签: java jsp servlets


    【解决方案1】:

    您正在发送获取参数,将您的代码从 doPost() 移动到 doGet() 方法,如下所示

    @WebServlet("/BrightSymphonyController")
    public class BrightSymphonyController extends HttpServlet {
    /**
     * @see HttpServlet#HttpServlet()
     */
    public BrightSymphonyController() {
        //super();
        // TODO Auto-generated constructor stub
    }
    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse       response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        String deleteuser = request.getParameter("delete");
            System.out.println("deleteuser============"+deleteuser);
            String id=request.getParameter("id");
            System.out.println("id============"+id);
    }
    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    
    
            }
    }
    

    【讨论】:

    • 是的@Zaheer Khorajiya 它的工作。你能解释一下servlet和jsp中的过程是如何进行的吗?到另一个 jsp 我也只发送获取参数 na.
    【解决方案2】:

    将您的代码从 doPost() 移动到 doGet()

    href 将请求发送为 get 而不是 post ,您没有使用 form 发送请求 post 您的请求是从 href 发送的

    如果您希望以post 发送请求

    <form method="post" action="./BrightSymphonyController" >
     <%
        for(int i=0;i<list.size();i++){
      %>
        <table id="example" class="display" cellspacing="0" width="100%">
          <thead>
             <tr>
               <th style="display: none;">ID</th>
             </tr>
          </thead>
          <tbody>
            <td>
              <input type="submit" class="urlImgDelete" value="Delete" /> 
              <input type="hidden" name="id" value="<%=list.get(i).getId()%>"/>
              <input type="hidden" name="delete" value="Delete" />
            </td>
        <%}%>
        </tbody>
        </table>
    </form>
    

    【讨论】:

      【解决方案3】:

      写这个

      <a href="<%=request.ContextPath()%>/BrightSymphonyController?id=<%=list.get(i).getId()%>&delete=Delete" class="urlImgDelete" title="Delete">
      

      插入

        <a href="./BrightSymphonyController?id=<%=list.get(i).getId()%>&delete=Delete" class="urlImgDelete" title="Delete">
      

      我希望这会有所帮助!!!!

      【讨论】:

      • 它显示这个错误--“方法 ContextPath() 未定义类型 HttpServletRequest”。
      • 抱歉我的错误是getContextPath()
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-06-04
      • 2013-01-28
      • 2016-03-14
      • 2012-10-22
      • 2014-02-25
      • 1970-01-01
      相关资源
      最近更新 更多