问题?在我们用表单的时候时常会遇到封装一些用户密码之类的数据这一个问题。
这里就交给大家几个便捷方法。有常用的封装方法,也有利用开源框架BeanUtilse框架来封装方法的。
举个例子:
然后是映射文件xml
这里就交给大家几个便捷方法。有常用的封装方法,也有利用开源框架BeanUtilse框架来封装方法的。
说明:
常用表单数据的获取
1.表单输入域类型:
radio checkbox,即使表单中有对应名称输入域,如果一个不选择,则什么数据不会带给服务器。(注 意空指针异常)
如果选择了其中的一个或多个,则把他们的value的取值提交给服务器。
如果选择了其中的一个或多个,他们又没有value取值,则提交给服务器的值是on.
2.请求参数的编码:
浏览器当前使用什么编码,就以什么编码提交请求参数。<meta http-equiv="content-type" content="text/html; charset=UTF-8">
request.setCharacterEncoding(编码):通知程序,客户端提交的数据使用的编码。但是只对POST请 求方式有效
3.如果是get请求提交数据,编码就是ISO-8859-1
4.Tips:目前采用POST提交方式。
举个例子:
再举封装方法之前,我们先看看HttpServletRequest中的request对象有哪些方法
package com.dp.java.Rquest;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* HttpRequest的常用方法的使用
*
*/
public class RequestDom1 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
PrintWriter pr=response.getWriter();
String url=request.getRequestURL().toString();
//协议:主机和端口 http://localhost:8080/day7/servlet/RequestDom1
String uri=request.getRequestURI();
//得到请求资源地址/day7/servlet/RequestDom1
String queryString =request.getQueryString();//get请求的参数字符串:如;表单中的username、password
String remoteAddr =request.getRemoteAddr();
int remotePort= request.getRemotePort();//得到客户端的端口号,注:不是服务器端口号8080
String method =request.getMethod();//请求方式走的是什么方法:如get或者post
pr.println("URL:"+url);
pr.println("uri:"+uri);
pr.println("queryString:"+queryString);
pr.println("remoteAddr:"+remoteAddr);
pr.println("remotePort:"+remotePort);
pr.println("method:"+method);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
首先是jsp或者html文件。
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<form action="/day7/servlet/RequestDom2" method="post"> <br>
用 户 名:<input type="text" name="username"> <br>
密 码:<input type="password" name="password"> <br>
确认密码:<input type="password" name="password"> <br>
性 别:<input type="text" name="gen"> <br>
<input type="submit" value="提交">
</form>
</body>
</html>
然后是映射文件xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"