1、中文字符的处理
//对于post方式提交的中文字符
request.setCharacterEncoding("utf-8");
String name= request.getParameter("name");
//对于get方式提交的中文字符
String rawName = request.getParameter("rawName");
//对于使用ISO-8859编码(默认编码方式传递过来的中文字符,分解转化为字节数组
byte[] rawBytes = rawName.getBytes("ISO-8859");
//将字节数组重新编码为字符串
String name2= new String(rawBytes,"utf-8");
2、过滤器Filter过滤编码,及用户权限的管理
@WebFilter(filterName="authority",urlPatterns={"/*"},initParams={@WebInitParam(name="encoding",value="utf-8"),
@WebInitParam(name="loginPage",value="/login.jsp"),@WebInitParam(name="proLogin",value="/proLogin.jsp")})
public class TestFilter implements Filter {
private FilterConfig config;
/**
* Default constructor.
*/
public TestFilter() {
// TODO Auto-generated constructor stub
}
/**
* @see Filter#destroy()
*/
public void destroy() {
// TODO Auto-generated method stub
this.config = null;
}
/**
* @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
*/
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
// TODO Auto-generated method stub
// place your code here
String encoding = config.getInitParameter("encoding");
String loginPage = config.getInitParameter("loginPage");
String proLogin = config.getInitParameter("proLogin");
request.setCharacterEncoding(encoding);
HttpServletRequest req = (HttpServletRequest)request;
HttpSession session = req.getSession(true);
//获取用户请求的页面
String requestPath = req.getServletPath();
//如果Session中的user为空,即表明用户还没有登录
//而且用户请求的既不是登录页面,也不是处理登录的页面
if(session.getAttribute("user") == null && !requestPath.endsWith(loginPage) && !requestPath.endsWith(proLogin)){
request.setAttribute("tip", "您好没有登录");
request.getRequestDispatcher(loginPage).forward(request, response);
}else{
chain.doFilter(request,response);
}
// pass the request along the filter chain
chain.doFilter(request, response);
}
/**
* @see Filter#init(FilterConfig)
*/
public void init(FilterConfig fConfig) throws ServletException {
// TODO Auto-generated method stub
this.config = fConfig;
}
}
4、区分
web application 名称为news,你在浏览器中输入请求路径: http://localhost:8080/news/main/list.jsp 则执行下面向行代码后打印出如下结果: 1、 System.out.println(request.getContextPath()); 打印结果:/news 2、System.out.println(request.getServletPath()); 打印结果:/main/list.jsp 3、 System.out.println(request.getRequestURI()); 打印结果:/news/main/list.jsp 4、 System.out.println(request.getRealPath("/")); 打印结果: F:\Tomcat 6.0\webapps\news\test
5、上传图片等文件的源代码(通用,使用Servlet3.0提供的part类)
@MultipartConfig
@WebServlet("/FileServlet")
public class FileServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public FileServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
//用户上传的文件名称
String fileName = request.getParameter("name");
//上传的为input为file,name指定为file
Part part =request.getPart("file");
//分别获取文件的类型、名称、大小
String fileType = part.getContentType();
long fileSize = part.getSize();
String rawName = part.getName();
//将文件写入磁盘中
part.write(getServletContext().getRealPath("/uploadFiles")+"/"+fileName);
}
}