import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Deme01 extends HttpServlet{
public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException{
//浏览器请求utf-8编码格式内容
request.setCharacterEncoding("UTF-8");
//服务器响应给浏览器的utf-8编码格式风格
response.setContentType("text/html;charset=UTF-8"); //html的文本格式,utf-8的字符集
//获取表单中所有提交的数据
String username = request.getParameter("username");
String password = request.getParameter("password");
String sex = request.getParameter("sex");
String[] hobbys = request.getParameterValues("hobby");
if(hobbys != null){
for(Sring string:hobbys){
System.out.println(string);
}
}
String city = request.getParameter("city");
//打印表单信息
System.out.println(username);
System.out.println(password);
System.out.println(sex);
System.out.println(city);
}
publc void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException{
doGet(request,response);
}
//1.2 使用对象接受打印表单
//1.创建User对象
package come.lanou3g;
import java.util.Arrays;
public class User{
//建议属性名和表单中的name一致
private String username;
private String password;
private String sex;
private String[] hobby;
private String city;
public User(){
super();
//TODO Auto-generated constructor stub
}
public User(String username,String password,String sex,String[]hobby,String city){
super();
this.username = username;
this.password = password;
this.sex = sex;
this.hobby = hobby;
this.city = city;
}
@override
//重写(Override)
重写是子类对父类的允许访问的方法的实现过程进行重新编写, 返回值和形参都不能改变。即外壳不变,核心重写!
重写的好处在于子类可以根据需要,定义特定于自己的行为。 也就是说子类能够根据需要实现父类的方法。
重写方法不能抛出新的检查异常或者比被重写方法申明更加宽泛的异常。例如: 父类的一个方法申明了一个检查异常 IOException,但是在重写这个方法的时候不能抛出 Exception 异常,因为 Exception 是 IOException 的父类,只能抛出 IOException 的子类异常。
在面向对象原则里,重写意味着可以重写任何现有方法。
public String toString(){
return "User[username="+username+",password="+password+",sex"+sex+",hobby="
+Arrays.toString(hobby)+",city="+city+"]";
}
//类的成员方法
public String getUsername(){
return username;
}
public void setUsername(String username){
this.username = username;
}
public String getPassword(){
return password;
}
public void setPassword(String password){
this.password = password;
}
public String getSex(){
return sex;
}
public void setSex(String sex)
this.sex = sex;
}
public String[] getHobby(){
return hobby;
}
public void setHobby(String[] hobby){
this.hobby = hobby;
}
public String getCity(){
return city;
}
public void setCity(String city){
this.city = city;
}
}