2014-09-05 60 views
0

我想显示数据而不传递参数到servlet。 就像当我进入这个网址显示没有通过的参数参数到Servlet

“本地主机:30050/AssigmentByMVC/RetriveByClassOrName”

,以便它告诉我的数据。 我试过,但它并没有显示,而通过这种方式显示数据

本地主机:30050/AssigmentByMVC/RetriveByClassOrName 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> 

这是

的ManipulationPage.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> 
+1

你如何调用你的servlet?你需要通过POST调用它。最简单的方法是创建来自与您的参数相关的输入。 – Gas 2014-09-05 10:03:52

+0

我没有调用Jsp页面,我直接调用servlet,然后通过requestDispatcher我包含了jsp页面 – 2014-09-05 10:05:29

+0

但是不知何故,你需要传递你使用的这些参数:SearchByStudent,SearchByClass。您可以通过url - ?SearchByClass ...,通过表单或AJAX调用的输入来完成它(但这可能暂时过于简单)。 ManipulationPage.jsp的外观如何? – Gas 2014-09-05 10:21:30

回答

0

您正在使用的表单提交方法:

<form action="${formActionURL}" method="post" id="searchForm"> 

所以,你需要将你的业务逻辑处理在servlet搜索到doPost()方法。

doGet()中,当用户单击存储数据页面上的链接时,您只需将逻辑转发保留为空ManipulationPage.jsp即可。

+0

好的解决了它。非常感谢Gas。只是将其他部分代码转移到doGet()方法,而其余部分则在后工作方法中,因为我希望工作正常 – 2014-09-05 11:00:23