上一篇:JavaWeb学习笔记(二)IDEA中创建maven项目以及导包方法
步骤:
1. 创建一个简洁的普通maven 项目(删掉src文件夹,建立module,并配置tomcat)
2. 在pom.xml中导入所需的依赖
即javax.servlet-api 和 jsp-api(去maven仓库找,不一定要用最多人使用的版本,用最新的)
3. 配置web.xml(优化)
- maven默认的webapp版本太低,从tomcat中复制过来替换掉
4. 创建前面提到的java和resource文件夹
5.在java文件夹中新建一个servlet类
(1)继承HttpServlet
(2)在IDEA中按ctrl+O快捷键,重写HttpServlet的doGet和doPost方法
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
public class HelloServlet extends HttpServlet {
//因为get和post知识请求实现的不同方式,可以相互调用,业务逻辑一样
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//super.doGet(req, resp);
PrintWriter writer = resp.getWriter();//相应流
writer.print("我被输出了");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//super.doPost(req, resp);
}
}
6. 在web.xml中编写Servlet的映射
因为,需要给浏览器一个访问java程序的路径
当浏览器访问/hello,找到helloServlet,再找到对应的类为 HelloSevlet
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<!--<web-app>-->
<!-- <display-name>Archetype Created Web Application</display-name>-->
<!--</web-app>-->
<web-app xmlns="https://jakarta.ee/xml/ns/jakartaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee
https://jakarta.ee/xml/ns/jakartaee/web-app_5_0.xsd"
version="5.0">
<!--注册servlet-->
<servlet>
<servlet-name>helloServlet</servlet-name>
<servlet-class>HelloServlet</servlet-class>
</servlet>
<!--一个servlet对应一个mapping-->
<servlet-mapping>
<servlet-name>helloServlet</servlet-name>
<!--请求路径-->
<url-pattern>/hello</url-pattern>
</servlet-mapping>
</web-app>
其他可能的路径写法:
如果想要在访问错误时,跳转到固定的错误页面,就在最下面写通配符
<servlet>
<servlet-name>error</servlet-name>
<servlet-class>errorServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>error</servlet-name>
<!--请求路径-->
<url-pattern>/*</url-pattern>
</servlet-mapping>
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
public class errorServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// super.doGet(req, resp);
resp.setContentType("text/html");
resp.setCharacterEncoding("utf-8");
PrintWriter writer= resp.getWriter();
writer.print("<h1>404</h1>");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// super.doPost(req, resp);
}
}
7. 点击执行
(1) 之前tomcat配置的路径是\sevlet_1,监听8080端口,等待浏览器Http请求,解析url地址,后面无内容时,默认访问index.jsp
(2) 路径\sevlet_1后面有内容时,监听8080端口,等待浏览器Http请求,解析url地址,在web.xml中匹配到对应的servlet,根据请求信息构建request对象,构建响应对象response,构建该servlet实例对象,通过Service方法,调用doGet(request,response)
失败的可能解决方法:
(1)tomcat版本不合适
(2)要将super方法注释掉