码迷,mamicode.com
首页 > 其他好文 > 详细

简单servlet示例

时间:2014-10-19 18:30:49      阅读:223      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   io   os   ar   java   for   

bubuko.com,布布扣

bubuko.com,布布扣

 

 1 package com.hill.temp;
 2 
 3 import java.io.IOException;
 4 import java.util.List;
 5 
 6 import javax.servlet.ServletException;
 7 import javax.servlet.http.HttpServlet;
 8 import javax.servlet.http.HttpServletRequest;
 9 import javax.servlet.http.HttpServletResponse;
10 
11 public class TestServlet extends HttpServlet {
12 
13     @Override
14     protected void service(HttpServletRequest req, HttpServletResponse res)
15             throws ServletException, IOException {
16                 //取得表单数据
17                 String username = req.getParameter("username");
18                 UserManager usermanager = new UserManager();
19                 List userList = usermanager.findUserByName(username);
20                 req.setAttribute("userlist", userList);
21                 //转向到c。jsp,取出List中的数据
22                 req.getRequestDispatcher("/a/b/c/c.jsp").forward(req, res);
23     }
24 
25 }
package com.hill.temp;

import java.util.ArrayList;
import java.util.List;

public class UserManager {
	public List findUserByName(String name){
		List userList = new ArrayList();
		userList.add("数据一");
		userList.add("数据二");
		return userList;
	}
}

  

b.jsp

<%@ page language="java" contentType="text/html; charset=GB18030"
    pageEncoding="GB18030"%>
<!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=GB18030">
<title>Insert title here</title>
</head>
<body>
	 <p>
	 <a href="../a.jsp">a.jsp</a><br>
  	<a href="c/c.jsp">c.jsp</a><br>
  	<a href="../d/d.jsp">d.jsp</a><br>
  	<a href="../../e/e.jsp">e.jsp</a><br>
</body>
</html>

  

c.jsp

<%@ page language="java" contentType="text/html; charset=GB18030"
    pageEncoding="GB18030"%>
<%@ page import="java.util.*"%>
<!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=GB18030">
<title>Insert title here</title>
</head>
<body>
	c.jsp
	<p>
   		<%
   			List userList = (List)request.getAttribute("userlist");
   			out.println(userList);
  		 %>
</body>
</html>

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_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>TestServlet</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  <servlet>
  	<servlet-name>TestServlet</servlet-name>
  	<servlet-class>com.hill.temp.TestServlet</servlet-class>
  </servlet>
  
  <servlet-mapping>
  	<servlet-name>TestServlet</servlet-name>
  	<url-pattern>/servlet/TestServlet</url-pattern>
  </servlet-mapping>
</web-app>

index.jsp

<%@ page language="java" contentType="text/html; charset=GB18030"
    pageEncoding="GB18030"%>
<!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=GB18030">
<title>Insert title here</title>
</head>
<body>
<h1>测试servlet</h1>
    <hr>
    <form action="servlet/TestServlet" method="post">
    	<input type="text" name="username"><br>
    	<input type="submit" value="查询"><br>
    </form>
    <a href="a/b/b.jsp">b.jsp</a><br>
</body>
</html>

  

 

  

 

简单servlet示例

标签:style   blog   http   color   io   os   ar   java   for   

原文地址:http://www.cnblogs.com/tianc-y/p/4035199.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!