第一个表单(Head first servlet+Jsp)(实验一)
第一个表单的HTML
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>BeerSel</title>
</head>
<body>
<h1 align="center">Beer Selection Page</h1>
<form method="POST" action="SelectBeer.do">
Select beer charateristics<p>
Color:
<select name="color" size="1">
<option value="light">light</option>
<option value="amber">amber</option>
<option value="brown">brown</option>
<option value="dark">dark</option>
</select>
<br><br>
<center>
<input type="submit">
</center>
</form>
</body>
</html>
2.在开发环境中创建DD
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>BeerSelect</display-name>
<welcome-file-list>
<welcome-file>form.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>BeerSelect</servlet-name>
<servlet-class>com.example.web.BeerSelect</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>BeerSelect</servlet-name>
<url-pattern>/SelectBeer.do</url-pattern>
</servlet-mapping>
</web-app>
3.控制器Servlet的第一版
package com.example.web; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Servlet implementation class BeerSelect */ @WebServlet("/BeerSelect") public class BeerSelect extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public BeerSelect() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub response.getWriter().append("Served at: ").append(request.getContextPath()); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.println("Beer Selection advice<br>"); String c = request.getParameter("color"); out.println("<br>got beer color:"+c); } }
将项目添加至tomcat,启动tomcat。
在浏览器进行测试。
选择颜色后点击 提交查询
改进这个servlet,让其调用模型
1.创建一个模型类
package com.example.model; import java.util.ArrayList; import java.util.List; public class BeerExpert { public List getBrands(String color){ List brands = new ArrayList(); if(color.equals("amber")){ brands.add("Jack Amber"); brands.add("Red Moose"); } else { brands.add("Jail Pale Ale"); brands.add("Gout Stout"); } return(brands); } }
2.改进servlet
package com.example.web; import java.io.IOException; import java.io.PrintWriter; import java.util.Iterator; import java.util.List; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.example.model.BeerExpert; /** * Servlet implementation class BeerSelect */ @WebServlet("/BeerSelect") public class BeerSelect extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public BeerSelect() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub response.getWriter().append("Served at: ").append(request.getContextPath()); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub String c = request.getParameter("color"); BeerExpert be = new BeerExpert(); List result = be.getBrands(c); response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.println("Beer Selection advice<br>"); Iterator it = result.iterator(); while(it.hasNext()) { out.println("<br>try:"+it.next()); } } }
测试效果
提交后
改进这个servlet,让其调用JSP返回
1.返回的JSP页面
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <%@ 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=ISO-8859-1"> <title>Beer advice</title> </head> <body> <h1 align="center">Beer Recomendations</h1> <p> <% List styles = (List)request.getAttribute("styles"); Iterator it = styles.iterator(); while(it.hasNext()){ out.print("<br>try:"+it.next()); } %> </body> </html>
2.改进后的Servlet
package com.example.web; import java.io.IOException; import java.io.PrintWriter; import java.util.Iterator; import java.util.List; import javax.servlet.RequestDispatcher; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.example.model.BeerExpert; /** * Servlet implementation class BeerSelect */ @WebServlet("/BeerSelect") public class BeerSelect extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public BeerSelect() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub response.getWriter().append("Served at: ").append(request.getContextPath()); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub String c = request.getParameter("color"); BeerExpert be = new BeerExpert(); List result = be.getBrands(c); // response.setContentType("text/html"); // PrintWriter out = response.getWriter(); // out.println("Beer Selection advice<br>"); // Iterator it = result.iterator(); // while(it.hasNext()) // { // out.println("<br>try:"+it.next()); // } request.setAttribute("styles", result);//为请求对象增加一个属性供JSP使用 RequestDispatcher view = request.getRequestDispatcher("advice.jsp"); //为JSP实例化一个请求分派器 view.forward(request, response);//使用请求分派器要求容器准备好JSP,并向JSP发送请求和响应 } }
重新启动tomcat
测试结果: