实验要求
- 按课程讲解创建servlet,并在servlet生命周期各函数内执行能想象的操作,可以向系统控制台输出相关内容,使用前面form表单的例子,把在jsp的实现改在servlet中再尝试实现。
- 创建欢迎页面,并设置欢迎页面
- 设置全局和局部参数,并通过不同的servlet去读取和展示相应参数
实验过程
- 创建servlet,servlet生命周期各函数向系统控制台输出相关内容,在servlet中使用form表单
(1)代码编写
welcome.java文件
package servlets;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class WelcomeServlet extends HttpServlet{
public WelcomeServlet(){
System.out.println("WelcomeServletg 构造函数");
}
@Override
public void init() throws ServletException {
System.out.println("WelcomeServlet.init 函数");
}
protected void doGet(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException{
// response.setContentType("text/html;charset=gb2312");
// PrintWriter out = response.getWriter();
// out.println("欢迎来到本系统!");
System.out.println("WelcomeServlet.doGet 函数");
}
protected void doPost(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException{
String account = request.getParameter("account");
System.out.println("WelcomeServlet.doPost 函数");
System.out.println("用户"+account+"请求服务");
}
@Override
public void destroy() {
System.out.println("WelcomeServlet.dostroy 函数");
}
}
提交表单post.jsp 代码
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>post请求服务</title>
</head>
<body>
请输入用户名:
<form action="servlets/WelcomeServlet" method="post">
<input type="text" name="account"><br>
<input type="submit" value="请求服务">
</form>
</body>
</html>
web.xml文件配置(完整web.xml在文章结尾)
<servlet>
<servlet-name>WelcomeServlet</servlet-name>
<servlet-class>servlets.WelcomeServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>WelcomeServlet</servlet-name>
<url-pattern>/servlets/WelcomeServlet</url-pattern>
</servlet-mapping>
(2)效果展示
通过post.jsp页面向servlet提交表单,使用posr请求
![](https://i-blog.csdnimg.cn/blog_migrate/cdc06ed9c9765ecddf058e2d2263db95.png#pic_center)
在浏览器地址栏中直接输入http://localhost:8080/pro8/servlets/WelcomeServlet 使用get请求
在IEDA中关闭Web服务器,servlet实例消亡,IDEA控制台输出如下
在servlet生命周期各函数执行了相应的操作,在控制台输出。Post方式成功提交表单至servlet。
2. 创建欢迎页面,并设置欢迎页面
(1)代码编写
welcome.jsp文件
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>欢迎页面</title>
</head>
<body>
欢迎来到本系统<br>
</body>
</html>
web.xml文件配置(完整web.xml在文章结尾)
<welcome-file-list>
<welcome-file>welcome.jsp</welcome-file>
</welcome-file-list>
(2)网页效果展示
在浏览器地址栏中直接输入http://localhost:8080/pro8/
![](https://i-blog.csdnimg.cn/blog_migrate/66e63ff40750451ff2a6f6f10fe57f78.png#pic_center)
- 设置全局和局部参数,并通过不同的servlet去读取和展示相应参数
(1)代码编写
package servlets;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class InitServelt extends HttpServlet{
public void doGet(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException{
ServletContext application = this.getServletContext();
String encoding = application.getInitParameter("param_name");
System.out.println("全局参数param_name是:"+encoding);
String driverClassName = this.getInitParameter("local_name");
System.out.println("局部参数local_name是:"+driverClassName);
}
}
web.xml文件配置(完整web.xml在文章结尾)
<context-param>
<param-name>param_name</param-name>
<param-value>java</param-value>
</context-param>
<servlet>
<servlet-name>InitServlet</servlet-name>
<servlet-class>servlets.InitServelt</servlet-class>
<init-param>
<param-name>local_name</param-name>
<param-value>local_java</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>InitServlet</servlet-name>
<url-pattern>/servlets/InitServlet</url-pattern>
</servlet-mapping>
(2)效果展示
在浏览器地址栏中直接输入http://localhost:8080/pro8/servlets/InitServlet
![](https://i-blog.csdnimg.cn/blog_migrate/e254d262e6d4457d4bd428341f0da5de.png#pic_center)
![](https://i-blog.csdnimg.cn/blog_migrate/6fed493d6eeae12a778c0cc3a5ccb07b.png#pic_center)
<?xml version="1.0" encoding="GBK"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<welcome-file-list>
<welcome-file>welcome.jsp</welcome-file>
</welcome-file-list>
<context-param>
<param-name>param_name</param-name>
<param-value>java</param-value>
</context-param>
<servlet>
<servlet-name>InitServlet</servlet-name>
<servlet-class>servlets.InitServelt</servlet-class>
<init-param>
<param-name>local_name</param-name>
<param-value>local_java</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>InitServlet</servlet-name>
<url-pattern>/servlets/InitServlet</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>WelcomeServlet</servlet-name>
<servlet-class>servlets.WelcomeServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>WelcomeServlet</servlet-name>
<url-pattern>/servlets/WelcomeServlet</url-pattern>
</servlet-mapping>
</web-app>
项目文件结构
![](https://i-blog.csdnimg.cn/blog_migrate/18448d60dffb6d8119162eeff29b4627.png#pic_center)