首页 > 代码库 > Servlet小结

Servlet小结

//register.html


<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
<title>
insert item here!
</title>
</head>
<form action="LoginServlet" method="post">
User:<input type="text" name="user"/><br>
Password:<input type="password" name="password"/><br/>
<input type="submit" value=http://www.mamicode.com/"submit"/>
<br/><br/>
interesting:
<input type="checkbox" name="interesting" value=http://www.mamicode.com/"reading"/>Reading
<input type="checkbox" name="interesting" value=http://www.mamicode.com/"writing"/>writing
<input type="checkbox" name="interesting" value=http://www.mamicode.com/"sport"/>sport
<input type="checkbox" name="interesting" value=http://www.mamicode.com/"exercise"/>exercise
<input type="checkbox" name="interesting" value=http://www.mamicode.com/"claming"/>claming

</form>
</html>


//LoginServlet
import javax.servlet.Servlet;


public class HelloServlet implements Servlet{

public void destroy(){
System.out.println("destroy");
}

public ServletConfig getServletConfig(){
System.out.println("getServletConfig");
return null;
}

public String getServletInfo(){
System.out.println("getServletInfo");
return null;
}
public void init(ServletConfig config){
System.out.println("init");

}
public void service(ServletRequest request,ServletResponse response)throws 
ServletException,IOException{

System.out.println("请求来了!");
String user=request.getParameter("user");
String password=request.getParameter("password");
System.out.println(user+""+password);

//测试getParameterValues()
String interesting=request.getParameter("interesting");
System.out.println(interesting);
//只能获取到第一个值reading

String[] str=request.getParameterValues("interesting");
for(String interest:str){
System.out.println(interest);
}

//getParameterNames()方法测试

Enumeration enum=request.getParameterNames();
while(enum.hasMoreElements()){
String name=names.nextElement();
String value=http://www.mamicode.com/request.getParameter(name);
System.out.println(name+":"+values);
}

//getParameterMap()方法
Map<String,String[]> map=request.getParameterMap();
for(Map.Entry<String,String[]> entry:map.entrySet()){
System.out.println(entry.getKey()+":"+Arrays.asList(entry.getValues)));
}


//获取请求的URI:(注意强转)
HttpServletRequest httpServletRequest=(HttpServletRequest)request;
String RequestURI=requestServlet.getRequestURI();
System.out.println(requestURI);

//获取请求方式

String method=HttpServletRequest.getMethod();
System.out.println(method);
//获取请求参数的字符串
String queryString=httpServletRequest.getQueryString();
System.out.println(queryString);

//获取Servlet名字
String ServletPath=httpServletRequest.getServletPath();
System.out.println(ServletPath);

}
//Servlet配置文件web.xml
<servlet>
<servlet-name>LoginServlet</servlet>
<servlet-class>LoginServlet</servlet>
</servlet>
<servlet-mapping>
<servlet-name>LoginServlet</servlet>
<url-pattern>/LoginServlet</url-pattern>
</servlet-mapping>


}


//总结:如何在Servlet中获取请求信息
service()用于应答请求,因为每次请求都会调用service方法
ServletRequest:封装了请求信息,可以从中获取到任何请求信息
ServletResponse:封装了相应信息,如果想给用户什么响应,具体可以使用该接口的方法实现
ServletResponse和servletRequest是服务器给予实现的,并在服务器调用service方法时传入
·
ServletRequest:
获取请求参数:
java.lang.StringgetParameter(java.lang.String name) 
          Returns the value of a request parameter as a String, or null if the parameter does not exist.

java.util.Map getParameterMap() 
          Returns a java.util.Map of the parameters of this request.

java.util.EnumerationgetParameterNames() 
          Returns an Enumeration of String objects containing the names of the parameters contained in this request.

java.lang.String[]getParameterValues(java.lang.String name) 
          Returns an array of String objects containing all of the values the given request parameter has, or null if the parameter does not exist.
获取请求的URI:
getRequestURI();
获取请求方式:
getMethod();
获取请求参数的字符串:
getQueryString();
//获取Servlet映射路径
getServletPath();
//HttpServletRequest:
是ServletRequest的子接口,针对于http请求所定义,里面多包含了大量获取http请求相关的方法

//和Attribute相关的几个方法
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
ServletResponse对象
1.getWriter():返回一个PrinterWriter对象,调用对象的print()方法,打印在浏览器
PrintWriter out=response.getWriter();
out.println("hello!")//输出在浏览器端
2.getOutputStream()方法:
用于文件下载
3.设置响应的内容类型
response.setContentType("application/msword");//使用Word文档打开
4.HttpServletResponse子接口
void sendRedirect(String location):请求重定向
在web.xml 文件中设置两个web应用的初始化参数,user,password
定义一个login.html.里面定义两个请求字段,user,password发送请求道loginServlet
在创建一个loginServlet,在其中获取请求的user,password比对和web.xml文件中定义的请求参数是否一致,若一致,相应hello:xxx
若不一致,则显示sorry

Servlet小结