MVC实战
Can't just sit here and read all day
- [ ] 1. set up development
- [ ] 2. set up development following the servlet and jsp specs and Tomcat requirement
- [ ] 3. Writing. Compiling. deploying and running
Web App Content
- static content
- JSP pages
- Servlet classes
- the deplyment descriptor
- tag libraries
- JAR files
- Java class files
==V==
review the user's views
graph TD
A[proj-BeerAdvisor] --> B(beerV1)
B --> C1(web)
B --> C2(etc)
C1 --> |static content| C1A[form.html]
C2 --> |the deplyment descriptor| C2A[web.xml]
form.html
<html>
<body>
<h1 align="center">Beer</h1>
<form method="post" action="selectBeer.do">
<p>select beer characteristics</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>
<center>
<input type="submit">
</center>
</form>
</body>
</html>
为什么是POST不是GET?
use the dd web.xml to map com.example.web.BeerSelect
mapping the logical name to a servlet class file P78
web.xml
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0"
metadata-complete="true">
<display-name>Welcome to Tomcat</display-name>
<description>
Welcome to Tomcat
</description>
<servlet>
<servlet-name>Ch3 Beer</servlet-name>
<servlet-class>com.example.web.BeerSelect</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Ch3 Beer</servlet-name>
<url-pattern>/SelectBeer.do</url-pattern>
</servlet-mapping>
</web-app>
Deploying and test the opening page
graph TD
A[tomcat] --> B(webapps)
B --> C(beer)
C --> |static content| D1[form.html]
C --> D2(WEB-INF)
D2 --> |the deplyment descriptor| E1[web.xml]
此处有测试
==C==
BeerSelect.java
package com.example.web;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
public class BeerSelect extends HttpServlet {
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException{
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);
}
}
java的异常处理怎么长这样?
package 有点名字空间的意思。
import 和python很像,机制想来也不简单。
ContentType
API
graph TD
A(javax.servlet.http.) --> B1(HttpServlet)
A --> B2(HttpServletRequest)
A --> B3(HttpServletResponse)
B1 --> B1A(javax.servlet.GenericServlet)
B1A --> C(javax.servlet.)
B2 --> C
B3 --> C
C --> D1(Serverlet)
C --> D2(ServletRequest)
C --> D3(ServletResponse)