文章目录
一、Servlet开发入门
(一)实现第一个Servlet程序
目标:掌握如何使用IDEA工具开发Servlet程序
1、使用IDEA完成Servlet的开发
在实际开发中,通常都会使用IDEA(或Eclipse等)工具完成Servlet的开发,我们使用IDEA完成Servlet的开发,因为IDEA不仅会自动编译Servlet,还会自动创建web.xml文件信息,完成Servlet虚拟路径的映射。
(1)新建Web项目
选择IDEA主页的“Create New Project”选项,进入新建项目的界面。
在New Projec界面中,选择左侧栏的“Java”选项,然后勾选”Web Application”选项。选择完毕之后,单击”Next”按钮进入填写项目信息的界面。
在New Projec界面中,”Project name”选项用于指项目的名称,”Project localtion”选项用于指定Web项目的根目录。项目的根目录设置为D:\web_work\chapter04,将WebDemo作为Web项目的名称。设置完成之后,单击“Finish”按钮,进入开发界面。
修改Artifact名称 - WebDemo
编辑Tomcat服务器配置
切换到【Server】选项卡
启动服务器,查看效果
(2)创建Servlet类
新建net.huawei.servlet包
在net.huawei.servlet包里创建ServletDemo01类
此时IDEA工具会自动生成Servlet代码
为了更好地演示Servlet的运行效果,接下来在ServletDemo01的doGet()和doPost()方法中添加一些代码。在@WebServlet注解里设置urlPatterns属性值:urlPatterns = “/demo01”
package net.huawei.servlet;
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;
import java.io.PrintWriter;
/**
* 功能:Servlet演示类
* 作者:华卫
* 日期:2023年03月12日
*/
@WebServlet(name = "ServletDemo01", urlPatterns = "/demo01")
public class ServletDemo01 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 获取字符输出流
PrintWriter out = response.getWriter();
// 输出信息
out.print("<h1>Hello Servlet World~</h1>");
}
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
}
其实,Servlet本质上就是Java程序里嵌入了HTML页面
(3)启动Servlet
启动服务器,显示首页
在页面访问ServletDemo01类的url地址“localhost:8080/WebDemo/demo01”
对应关系图
希望显示红色消息,并且居中显示,那么就要用到样式
2、课堂练习 - 创建Web项目,利用Servlet显示个人信息
创建Java Enterprise项目
设置项目名称与保存位置
单击【Finish】按钮
看Artifacts的名称
将名称改为ShowInfo
配置tomcat服务器
在Deployment里删除ShowInfo后重新添加
切换到【Server】选项卡,查看URL
启动tomcat服务器,查看效果
修改首页文件,以表格形式显示个人信息
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>个人信息</title>
</head>
<body>
<table style="text-align: center" border="1" align="center" cellpadding="5">
<tr>
<td>学号</td>
<td>姓名</td>
<td>性别</td>
<td>年龄</td>
<td>专业</td>
<td>班级</td>
<td>手机</td>
</tr>
<tr>
<td>20210201</td>
<td>陈燕文</td>
<td>女</td>
<td>18</td>
<td>软件技术专业</td>
<td>2021软件2班</td>
<td>15890456780</td>
</tr>
</table>
</body>
</html>
启动服务器,查看结果
创建net.huawei.servlet包,在包里创建InfoServlet类
package net.huawei.servlet;
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;
import java.io.PrintWriter;
/**
* 功能:显示个人信息
* 作者:华卫
* 日期:2023年03月31日
*/
@WebServlet(name = "InfoServlet", urlPatterns = "/info")
public class InfoServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 获取字符输出流
PrintWriter out = response.getWriter();
// 往客户端输出信息
out.print("<!DOCTYPE html>");
out.print("<html>");
out.print("<head>");
out.print("<meta charset='UTF-8'");
out.print("<title>个人信息</title>");
out.print("</head>");
out.print("<body style='text-align: center'>");
out.print("<table border='1' align='center' cellpadding='5'>");
out.print("<tr>");
out.print("<td>学号</td>");
out.print("<td>姓名</td>");
out.print("<td>性别</td>");
out.print("<td>年龄</td>");
out.print("<td>专业</td>");
out.print("<td>班级</td>");
out.print("<td>手机</td>");
out.print("</tr>");
out.print("<tr>");
out.print("<td>20210201</td>");
out.print("<td>陈燕文</td>");
out.print("<td>女</td>");
out.print("<td>18</td>");
out.print("<td>软件技术专业</td>");
out.print("<td>2021软件2班</td>");
out.print("<td>15890456780</td>");
out.print("</tr>");
out.print("</table>");
out.print("</body>");
out.print("</html>");
}
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
}
启动tomcat服务器,访问http://localhost:8080/ShowInfo/info
页面出现中文乱码,需要设置字符编码(要查看Chrome浏览器默认的字符编码)
设置响应对象字符编码为UTF-8,与浏览器当前采用的字符编码保持一致
重启tomcat服务器,访问http://localhost:8080/ShowInfo/info
无论浏览器当前采用什么字符编码,通过设置响应对象内容类型来要求浏览器采用指定的字符编码
重启tomcat服务器,访问http://localhost:8080/ShowInfo/info
查看浏览器当前使用的字符编码,已经被改成GBK了
(二)Servlet的配置
目标:掌握完成Servlet的配置的两种方式:通过Web应用的配置文件web.xml来,通过@WebServlet注解来完成Servlet的配置
若想让Servlet正确地运行在服务器中并处理请求信息,必须进行适当的配置,关于Servlet的配置主要有两种方式,分别是通过Web应用的配置文件web.xml来完成配置和使用@WebServlet注解的方式完成。
1、使用web.xml配置Servlet
在web.xml文件中,通过标签进行注册,在标签下包含若干个子元素。
把Servlet映射到URL地址,使用标签进行映射,使用子标签指定要映射的Servlet名称,名称要和之前在标签下注册的相同;使用子标签映射URL地址,地址前必须加“/”,否则访问不到。
在部署描述文件web.xml里注册了Servlet,那么就可以注释掉InfoServlet类上的注解
重启tomcat服务器,访问http://localhost:8080/ShowInfo/info
2、@WebServlet注解属性
@WebServlet 注解用于代替web.xml文件中的等标签,该注解将会在项目部署时被容器处理,容器将根据具体的属性配置将相应的类部署为Servlet。为此,@WebServlet注解提供了一些属性。
@WebServlet注解可以标注在任意一个继承了HttpServlet类的类之上,属于类级别的注解。下面使用@WebServlet注解标注InfoServlet类。
在web.xml文件里注释掉对InfoServlet的注册标签