1、servlet裸奔
2、重写了Servlet的init方法后一定要记得调用父类的init方法,否则在service/doGet/doPost方法中使用getServletContext()方法获取ServletContext对象时就会出现java.lang.NullPointerException异常
3、转码支持中文显示
myform.html
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8">
<title>Insert title here</title>
</head>
<body>
<h1>表单数据提交</h1>
<form action="helloWorld" method="POST">
姓名:<input type="text" name="name_str"/><br/><br/>
职业:<input type="text" name="occyp_str"/><br/><br/>
年龄:<input type="text" name="age_num"/><br/><br/>
<input type="submit" value="提交"/>
</form>
</body>
</html>
HelloWorldServlet.java
package com.web1.hello;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
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 HelloWorldServlet
*/
@WebServlet("/HelloWorldServlet")
public class HelloWorldServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private String message;
/**
* @see HttpServlet#HttpServlet()
*/
public HelloWorldServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see Servlet#init(ServletConfig)
*/
public void init(ServletConfig config) throws ServletException {
message = "Hello world, Servlet Example!";
/*重写了Servlet的init方法后一定要记得调用父类的init方法,
否则在service/doGet/doPost方法中
使用getServletContext()方法获取ServletContext对象时
就会出现java.lang.NullPointerException异常*/
super.init(config);
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
/*response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<h1>" +message+ "</h1>");
out.close();*/
//最原始的页面跳转
String formPage = "/pages/myform.html";
RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(formPage);
dispatcher.forward(request, response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Enumeration<String> names = request.getParameterNames();
response.setHeader("Content-type", "text/html;charset=UTF-8");
response.setCharacterEncoding("UTF-8");
PrintWriter writer = response.getWriter();
writer.println("<h1>显示提交表单数据</h1>");
while (names.hasMoreElements()) {
String name = names.nextElement();
writer.print("<p>");
writer.print(name + " : ");
//在Tomcat中request和response的默认编码格式
//为ISO-8859-1
//转码成UTF-8
String value = request.getParameter(name);
value = new String(value.getBytes("ISO-8859-1"), "UTF-8");
writer.print(value);
writer.print("</p>");
}
writer.println("<h1>提交成功</h1>");
writer.close();
}
@Override
public void destroy() {
// TODO Auto-generated method stub
super.destroy();
}
}
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http;//www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0" metadata-complete="true">
<description>
Hello World Servlet
</description>
<display-name>Hello World Servlet</display-name>
<servlet>
<servlet-name>helloWorld</servlet-name>
<servlet-class>com.web1.hello.HelloWorldServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>helloWorld</servlet-name>
<url-pattern>/helloWorld</url-pattern>
</servlet-mapping>
</web-app>