-
简单实例
- 页面请求登陆,提交表单数据
<body>
<form action="loginServlet" method="get">
user:<input type="text" name="user"/>
<br>
password:<input type="password" name="password"/>
<br>
interesting:
<input type="checkbox" name="interesting" value="read"/>read
<input type="checkbox" name="interesting" value="walk"/>walk
<input type="checkbox" name="interesting" value="swing"/>swing
<input type="checkbox" name="interesting" value="shopping"/>shopping
<input type="checkbox" name="interesting" value="tv"/>TV
<br>
<input type="submit" value="Submit"/>
</form>
</body>
- web.xml配置文件
<servlet>
<servlet-name>loginServlet</servlet-name>
<servlet-class>servlet.loginServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>loginServlet</servlet-name>
<url-pattern>/loginServlet</url-pattern>
</servlet-mapping>
- loginServlet实现Servlet接口,用于处理登陆请求
public class loginServlet implements Servlet {
@Override
public void destroy() {
}
@Override
public ServletConfig getServletConfig() {
return null;
}
@Override
public String getServletInfo() {
return null;
}
@Override
public void init(ServletConfig arg0) throws ServletException {
}
@Override
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);
String interest = request.getParameter("interesting");
System.out.println(interest);
//获取指定参数名的一组参数值
String [] values = request.getParameterValues("interesting");
for(String value : values){
System.out.println("--"+value);
}
//获取Enumeration对象
Enumeration<String> names = request.getParameterNames();
while(names.hasMoreElements()){
String name = names.nextElement();
String value = request.getParameter(name);
System.out.println("^^" + name + ":" + value);
}
//获取参数值的键值对
Map<String, String[]> map = request.getParameterMap();
for(Map.Entry<String, String[]> entry : map.entrySet()){
System.out.println("**" + entry.getKey() + ": " + Arrays.asList(entry.getValue()));
}
//获取请求的URI
HttpServletRequest httpServletRequest = (HttpServletRequest)request;
String requestURI = httpServletRequest.getRequestURI();
System.out.println("requestURI:" + requestURI);
//获取请求的方式
String method = httpServletRequest.getMethod();
System.out.println("请求方式:" + method);
//获取请求的查询字符串(当然请求方式为get,否则哪来的请求参数)
String queryString = httpServletRequest.getQueryString();
System.out.println("查询字符串:" + queryString);
//获取Servlet的映射路径,就是配置在web.xml中的配置信息
String servletPath = httpServletRequest.getServletPath();
System.out.println("servletPath:" + servletPath);
//返回响应类型
response.setContentType("application/msword");
//返回响应信息
PrintWriter out = response.getWriter();
out.println("helloWorld");
}
}
- 获取到表单提交的数据
请求来了...
tengpengfei, 817425
read
--read
--walk
^^user:tengpengfei
^^password:817425
^^interesting:read
**user: [tengpengfei]
**password: [817425]
**interesting: [read, walk]
requestURI:/Demo4_servlet/loginServlet
请求方式:GET
查询字符串:user=tengpengfei&password=817425&interesting=read&interesting=walk
servletPath:/loginServlet
- 问题
因为继承Servlet从而必须实现接口中所有的方法,但有些方法
用不到,就会产生多余代码,为了解决这个问题,可以自己定义一个Servlet