MyRequest
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
/**对HttpServletRequest对象包装/装饰*/
public class MyRequest extends HttpServletRequestWrapper{
private HttpServletRequest request;
public MyRequest(HttpServletRequest request) {
super(request);
this.request = request;
}
//重写父类的方法
public String getParameter(String name) {//表单项的名字
String value = null;
//取得客户端的请求方式[GET/POST]
String method = request.getMethod();
if("GET".equals(method)){
try {
value = request.getParameter(name);//乱码
byte[] buf = value.getBytes("ISO8859-1");
value = new String(buf,"UTF-8");//正码
} catch (Exception e) {
e.printStackTrace();
}
}else if("POST".equals(method)){
try {
request.setCharacterEncoding("UTF-8");
value = request.getParameter(name);//正码
} catch (Exception e) {
e.printStackTrace();
}
}
value = filter(value);
return value;
}
//转义方法
public String filter(String message) {
if (message == null)
return (null);
char content[] = new char[message.length()];
message.getChars(0, message.length(), content, 0);
StringBuffer result = new StringBuffer(content.length + 50);
for (int i = 0; i < content.length; i++) {
switch (content[i]) {
case ‘<‘:
result.append("<");
break;
case ‘>‘:
result.append(">");
break;
case ‘&‘:
result.append("&");
break;
case ‘"‘:
result.append(""");
break;
default:
result.append(content[i]);
}
}
return (result.toString());
}
GetPostServlet
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;
public class GetPostServlet extends HttpServlet {
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doGet(request,response);
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String username = request.getParameter("username");
response.getWriter().write(username);
}
}
longin
<%@ 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>
<title>登陆页面</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="${pageContext.request.contextPath}/GetPostServlet" method="post">
<table border="1" align="center">
<caption>用户登陆</caption>
<tr>
<th>用户名</th>
<td><input type="text" name="username"/>
</td>
<td colspan="5" align="center">
<input type="submit" value="提交"/>
</td>
</tr>
</table>
</form>
</body>
</html>
原文地址:http://blog.51cto.com/357712148/2105506
时间: 2024-10-09 01:35:51