1. 程式人生 > >Jsp頁面請求-Servlet返回結果

Jsp頁面請求-Servlet返回結果

jsp頁面通過表單的方式提交請求,並傳進後臺引數,servlet接收請求再返回頁面顯示。

1.jsp頁面程式碼:

<%@ page language="java" contentType="text/html; charset=utf-8"  pageEncoding="utf-8" import="com.service.entity.*,java.util.*"%>  
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>  
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>學生資訊</title>
</head>
<body>
<style type="text/css">
table.dataintable {
	margin-top:15px;
	border-collapse:collapse;
	border:1px solid #aaa;
	width:100%;
	}

table.dataintable th {
	vertical-align:baseline;
	padding:5px 15px 5px 6px;
	background-color:#3F3F3F;
	border:1px solid #3F3F3F;
	text-align:left;
	color:#fff;
	}

table.dataintable td {
	vertical-align:text-top;
	padding:6px 15px 6px 6px;
	border:1px solid #aaa;
	}

table.dataintable tr:nth-child(odd) {
	background-color:#F5F5F5;
}

table.dataintable tr:nth-child(even) {
	background-color:#fff;
}
</style>
<form action ="StudentServlet" method="get">
輸入姓名:<input type="text" name="name"/>
<input type="submit" value="查詢" />
</form>
&nbsp;
&nbsp;</div>
<table class="dataintable">
<tr>
  <th>姓名</th>
  <th>語文</th>
  <th>數學</th>
  <th>英語</th>
</tr>

<c:forEach items="${list}" var="stu">  
    <tr>  
        <td><b>${stu.getName()}</b></td>  	
		<td>${stu.getChinese()}</td>  
        <td>${stu.getMath()}</td>   
    	<td>${stu.getEnglish()}</td>  
    </tr>  
</c:forEach> 
</body>
</html>

首先編碼都是utf-8,所以開頭需要修改:

<%@ page language="java" contentType="text/html; charset=utf-8"  pageEncoding="utf-8" import="com.service.entity.*,java.util.*"%>  

import="com.service.entity.*,java.util.*是引入了Java包,來訪問物件的成員變數。

<c:forEach items="${list}" var="stu">  
    <tr>  
        <td><b>${stu.getName()}</b></td>      
        <td>${stu.getChinese()}</td>  
        <td>${stu.getMath()}</td>   
        <td>${stu.getEnglish()}</td>  
    </tr>  
</c:forEach> 
然後jsp引入了c:forEach,所以需要加:

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>  

加了之後需要引入兩個jar包,可以去下載jstl.jar+standard.jar兩個包

2.Java Servlet程式碼

package com.service.servlet;

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

import com.service.StudentService;
@WebServlet("/StudentServlet")
public class StudentServlet extends HttpServlet{
	 private static final long serialVersionUID = 1L;  
	    
	    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {  
	        // TODO Auto-generated method stub 
	    	// 處理響應資料的格式和編碼,通知瀏覽器資料的顯示方式
	     response.setCharacterEncoding("utf-8");
	     response.setContentType("text/html;charset=utf-8");
	     PrintWriter pw = response.getWriter();  
         pw.println("doGet!"); 
         System.out.println ("doGet1");  
         String Name=request.getParameter("name");   
         StudentService ws = new StudentService();
	     List list =  ws.GetStudentInfo(Name); 
	     request.setAttribute("list",list);  
	     request.getRequestDispatcher("/student.jsp").forward(request, response);  
	    }  

	    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {  
	        // TODO Auto-generated method stub  
	    }  


}

首先@WebServlet("/StudentServlet")是web.xml配置的servlet名

<?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">
  <servlet>
        <servlet-name>StudentServlet</servlet-name>
        <servlet-class>com.service.servlet.StudentServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>StudentServlet</servlet-name>
        <url-pattern>/service</url-pattern>
    </servlet-mapping>
</web-app>

和jsp檔案裡:

這三個地方的這個名字是一一對應的,不能寫錯。

還有就是,傳進來的引數:

這就整個請求->接收請求->處理請求->返回的過程。

最後結果展示: