Servlets 在不向Servlet传递参数的情况下显示数据

Servlets 在不向Servlet传递参数的情况下显示数据,servlets,Servlets,我希望在不向servlet传递参数的情况下显示数据。 比如当我输入这个url时 本地主机:30050/assignmentbymvc/retrievebyclassorname 这样它就能显示数据了。 我试过了,但它没有显示,而通过这种方式显示数据 localhost:30050/assignmentbymvc/retrievebyclassorname?SearchByName=&SearchByClass= 我的Servlet代码是 package manipulationStudentD

我希望在不向servlet传递参数的情况下显示数据。 比如当我输入这个url时

本地主机:30050/assignmentbymvc/retrievebyclassorname

这样它就能显示数据了。 我试过了,但它没有显示,而通过这种方式显示数据

localhost:30050/assignmentbymvc/retrievebyclassorname?SearchByName=&SearchByClass=

我的Servlet代码是

 package manipulationStudentData;

 import java.util.*;
 import java.io.IOException;
 import javax.servlet.ServletException;
 import javax.servlet.annotation.WebServlet;
 import javax.servlet.http.HttpServlet;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;


 @WebServlet(description = "Retrive By Class Or Name", urlPatterns = {   
 "/RetriveByClassOrName" })

 public class RetriveByClassOrNameController extends HttpServlet {
 private static final long serialVersionUID = 1L;


 public RetriveByClassOrNameController() {
    super();

}


 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws   
 ServletException, IOException {

    ArrayList<ManipulateStudentBean> Search=new ArrayList<ManipulateStudentBean>();
    DatabaseActivityDao database=new DatabaseActivityDao();



/*#################################################################################*/
                    /*Searching By Student First Name*/          
/*#################################################################################*/


    if(request.getParameter("SearchByStudent") != "" && 
      request.getParameter("SearchByClass") != "" ){


Search=database.showOnClassandName(request.getParameter("SearchByStudent"),
                                   request.getParameter("SearchByClass"));

        request.setAttribute("ResultByClassAndName", Search);
        request.setAttribute("ResultByFilter", "both");
        request.getRequestDispatcher("/ManipulationPage.jsp")
        .include(request,  response);
    }


   /*################################################################################*/
                            /*Searching By Student First Name*/
  /*################################################################################*/

    else if(request.getParameter("SearchByStudent") != "" &&
            request.getParameter("SearchByClass") == ""){

        Search=database.showOnName(request.getParameter("SearchByStudent"));

        request.setAttribute("ResultSet", Search);
        request.setAttribute("ResultByFilter", "student");
        request.getRequestDispatcher("/ManipulationPage.jsp")
        .include(request, response);
    }

    /*##########################################################################*/
                            /*Searching By Student First Name*/
    /*##########################################################################*/

    else if(request.getParameter("SearchByStudent") == "" && 
            request.getParameter("SearchByClass") != ""){

        Search=database.showOnClass(request.getParameter("SearchByClass"));

        request.setAttribute("ResultSet", Search);
        request.setAttribute("ResultByFilter", "class");
        request.getRequestDispatcher("/ManipulationPage.jsp")
        .include(request, response);
    }
    /*###############################################################*/
                        /*Searching By Student First Name*/
    /*################################################################*/

    else if(request.getParameter("SearchByStudent") == "" && 
            request.getParameter("SearchByClass") == "" ){

        Search=database.showStudentData();

        request.setAttribute("ResultSet", Search);
        request.getRequestDispatcher("/ManipulationPage.jsp")
        .include(request, response);
    }

    else{

        Search=database.showStudentData();

        request.setAttribute("ResultSet", Search);
        request.getRequestDispatcher("/ManipulationPage.jsp")
        .include(request, response);
    }

  }


   protected void doPost(HttpServletRequest request, HttpServletResponse response) 
   throws ServletException, IOException {
    doGet(request,response);
  }

}
从这里我叫它

<html>
   <head>
       <title>Storing Data </title>
       <link rel="stylesheet" type="text\css" href="Index.css" />
  </head>
   <body>
       <div id="links">
           <h3>To Add Students Goto This Link</h3>

           <h4><a href="/ManipulationPage.jsp" > Students </a> </h4>

           <h3>For Updating Deleting Student Record go to this Page</h3>
           <h4> <a href="/AssigmentByMVC/RetriveByClassOrName" > Manipulate </a> </h4>
      </div>
  </body>
</html>
这是OperationPage.jsp页面

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>



<html>
<head>
<title>Manipulation Page</title>
<link rel="stylesheet" type="text\css" href="ManipulationPage.css"> 
</head>
<body>
<div id="formData">
    <c:url var="formActionURL" value="/RetriveByClassOrName" />
    <form action="${formActionURL}" method="post" id="searchForm">
    <p>
    <label for="SearchByStudent"> Student First Name </label>
    <input type="text" name="SearchByStudent" />
    </p>

    <p>
    <label for="SearchByClass">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Student Class </label>
    <input type="text" name="SearchByClass" />
    </p>

    <p>
    <input type="submit" value="Search"/>
    </p>
</form>
</div>


<div id="showData">
    <table id="table" cellpadding="3px" border="1px">
        <tr>
            <th>ID</th>
            <th>First Name</th>
            <th>Last Name</th>
            <th>Father Name </th>
            <th>Gender </th>
            <th>Date Of Birth</th>
            <th>Class</th>
            <th>Address</th>
            <th>Subjects</th>
            <th>Update</th>
            <th>Delete</th>
        </tr>       
        <c:forEach var="data" items="${ResultSet}">
            <tr>
                <td><c:out value="${data.studentId}"></c:out></td>
                <td><c:out value="${data.firstName}"></c:out></td>
                <td><c:out value="${data.lastName}"></c:out></td>
                <td><c:out value="${data.fatherName}"></c:out></td>
                <td><c:out value="${data.gender}"></c:out></td>
                <td><c:out value="${data.dob}"></c:out></td>
                <td><c:out value="${data.classNo}"></c:out></td>
                <td><c:out value="${data.address}"></c:out></td>
            </tr>
        </c:forEach>

    </table>
</div>

</body>
</html> 

您正在表单中使用post方法:

 <form action="${formActionURL}" method="post" id="searchForm">
因此,您需要将servlet中的业务逻辑处理搜索移动到doPost方法


在doGet中,当用户单击存储数据页上的链接时,您只需将逻辑转发留给空的operationPage.jsp。

您如何调用servlet?您需要通过POST调用它。最简单的方法是使用与参数相关的输入从中创建。我不是调用Jsp页面,而是直接调用servlet,然后通过requestDispatcher包含Jsp页面,但不知何故,您需要传递您使用的这些参数:SearchByStudent、SearchByClass。您可以通过url-?SearchByClass,通过表单或AJAX调用中的输入,但这可能暂时太高级了。你的manufactionPage.jsp看起来怎么样?我已经更新了post GasOk解决了这个问题。多谢加油。刚刚将else部分代码转移到doGet方法,而剩下的部分在post方法中,该方法按照我的要求正确工作