首页 > 代码库 > 简单servlet示例
简单servlet示例
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 @Override14 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="http://www.mamicode.com/a.jsp">a.jsp</a><br> <a href="http://www.mamicode.com/c/c.jsp">c.jsp</a><br> <a href="http://www.mamicode.com/d/d.jsp">d.jsp</a><br> <a href="http://www.mamicode.com/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="http://www.mamicode.com/查询"><br> </form> <a href="http://www.mamicode.com/a/b/b.jsp">b.jsp</a><br></body></html>
简单servlet示例
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。