上一篇我们已经用eclipse搭建好了一个maven工程,并转换成了web工程,现在我们讨论web工程中较常见的问题
1、jsp页面中文乱码问题
当我们将创建好的web工程部署到tomcat上,并在浏览器上访问 http://localhost:8080/MyWeb/ 时,发现页面显示的是乱码,index.jsp 内容如下
首页
解决方法是在index.jsp中添加 ,修改后的index.jsp内容如下
contentType="text/html;charset=utf-8"%>
首页
2、创建一个servlet ,并通过servlet配置的url访问该servlet
(1)新建一个类LoginServlet,继承HttpServlet,并重写doGet和doPost方法,并将请求转向jsp页面,如下
package com.fanghao;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 8859563008113458339L;
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//设置请求要跳转到的页面为/WEB-INF/jsp/login.jsp,此处采用的是相对路径,webapp文件夹就相当于是工程根路径
request.getRequestDispatcher("/WEB-INF/jsp/login.jsp").forward(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
(2)在web.xml中配置该servlet,web.xml内容如下
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
Archetype Created Web Application
loginServlet
com.fanghao.LoginServlet
loginServlet
/login
每一个servlet都需要配置一组和,如果有多个servlet,就配置多组,其中的配置的就是访问该servlet使用的url
(3)在工程的WEB-INF文件夹下的jsp文件夹下创建一个login.jsp即可,login.jsp内容如下
contentType="text/html;charset=utf-8"%>
登录页面欢迎访问登录页面
(3)实现servlet和jsp页面之间的相互跳转
修改以上的login.jsp,内容如下
contentType="text/html;charset=utf-8"%>
登录页面用户名:
其中form表单的action属性表示的就是当我们点击提交按钮时,该表单的内容被提交到哪个url地址,${pageContext.request.contextPath }是使用EL表达式取出pageContext.request.contextPath表示的值,就是当前工程名,即对于当前项目而言 ${pageContext.request.contextPath } 等价于 /MyWeb
我们可以看到login.jsp出现红色报错:Multiple annotations found at this line:- javax.servlet.jsp cannot be resolved toa type
这是因为工程缺少servlet-api.jar和jsp-api.jar,添加这两个jar包即可解决,对应的maven依赖为
javax.servlet
servlet-api
2.5
provided
javax.servlet.jsp
jsp-api
2.2
provided
新建类LoginCheckServlet,继承HttpServlet,并重写doGet和doPost方法
package com.fanghao;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@SuppressWarnings("serial")
public class LoginCheckServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//客户端是以UTF-8编码传输数据到服务器端的,所以需要设置服务器端以UTF-8的编码方式接收,否则对于中文数据就会产生乱码
request.setCharacterEncoding("UTF-8");
//request.getParameter的key是与jsp页面上的输入框input的name属性一一对应的
String userName = request.getParameter("username");
System.out.println("用户登录验证通过,向数据库中插入数据:" + userName);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
在web.xml中添加该servlet的配置
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
Archetype Created Web Application
loginServlet
com.fanghao.LoginServlet
loginCheckServlet
com.fanghao.LoginCheckServlet
loginServlet
/login
loginCheckServlet
/doLoginCheck
以上就实现了servlet和jsp页面之间的相互跳转,可同理增加jsp页面和servlet实现更为复杂的程序