探究
- 一、如何获取当前项目名称
- 1.利用request对象的getContextPath()方法
- 2.利用jsp中的EL表达式
- 3. 具体代码验证实现
- 二、针对filter中的中文乱码问题(书上代码有问题)
- 1. 编写form.jsp
- 2.创建Servlet
- 3.创建过滤器
- 4.测试实现
编译环境
一、如何获取当前项目名称
1.利用request对象的getContextPath()方法
注意此方法是针对于java语言的
在jsp中使用<%=request.getContextPath()%>即可
2.利用jsp中的EL表达式
${pageContext.request.contextPath}
注意要插入该语句
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
3. 具体代码验证实现
首先在Webapp下编写jsp代码
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h2>进入当前文件夹下的getNameServlet</h2>
<a href="${PageContext.request.contextPath}/IDEA_war_exploded/getNameServlet"> click </a>
</body>
</html>
可以看到是要找到getNameServlet.java
所以在src中编写对应的Servlet,只给出主要代码
response.setContentType("text/html;charset=utf-8");
PrintWriter out=response.getWriter();
out.print("当前文件夹名称为:"+request.getContextPath());
运行结果
点击链接
二、针对filter中的中文乱码问题(书上代码有问题)
1. 编写form.jsp
<%--
Created by IntelliJ IDEA.
User: 86152
Date: 2021/11/30
Time: 15:38
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>用户登录</title>
<div style="text-align: center;">
<h3>用户登录</h3>
</div>
</head>
<body style="text-align: center;">
<a href="<%=request.getContextPath()%>/CharacterServlet?name=小朋友&password=123456">点击此处进行登录</a>
<form action="<%=request.getContextPath()%>/CharacterServlet" method="post">
<table border="1" cellspacing="0" cellpadding="0" width="80%" align="center">
<tr>
<td height="30" align="center">用户名</td>
<td><input type="text" name="name"></td>
</tr>
<tr>
<td height="30" align="center">密码</td>
<td><input type="text" name="password"></td>
</tr>
<tr
<td height="30" align="center" colspan="2">
<input type="submit" value="登录">
<input type="reset" value="重置">
</td>
</tr>
</table>
</form>
</body>
</html>
2.创建Servlet
package filter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet("/CharacterServlet")
public class CharacterServlet extends HttpServlet {
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//设置编码
req.setCharacterEncoding("utf-8");
System.out.println(req.getParameter("name"));
System.out.println(req.getParameter("password"));
}
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
3.创建过滤器
package filter;
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
public class CharacterFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
Filter.super.init(filterConfig);
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain chain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) servletRequest;
HttpServletResponse response = (HttpServletResponse) servletResponse;
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
CharacterRequest characterRequest = new CharacterRequest(request);
chain.doFilter(characterRequest, response);
}
@Override
public void destroy() {
Filter.super.destroy();
}
}
class CharacterRequest extends HttpServletRequestWrapper {
private final HttpServletRequest request;
public CharacterRequest(HttpServletRequest request) {
super(request);
this.request=request;
}
public String getParameter(String name){
//调用包装对象getParameter的方法,获得请求参数
String value=super.getParameter(name);;
if(value==null)return null;
String method=super.getMethod();
if("get".equalsIgnoreCase(method)){
try{
value=new String(value.getBytes("iso-8859-1"),"UTF-8");
}catch (UnsupportedEncodingException e){
throw new RuntimeException(e);
}
}
return value;
}
}
4.测试实现
当点击超链接时
观察左下角,观察使用了get方法
在控制台可以看到
输入登录时>
此时的控制台出现乱码
但是不知道为什么我的密码都不能传值进去了。。。
原因就是我在password的name属性写成了password="password"这就造成了找不到这个元素。
name属性 :元素的名称;这个不需要多解释了,也就是name的取值代表为当前input元素起个名字;
然后密码能传进去,但是汉字仍然乱码
接下来我在CharacterServlet中加入了 req.setCharacterEncoding(“utf-8”);
就能解决了乱码问题