eclipse 是不可能用的,并不是说它界面丑,也不是说它难用,而是它实在不适合我,idea 还能用一用,界面还挺符合我审美的,一些操作也比较合适。
注意:本篇文章使用的是工具是 IDEA,搭配 tomcat 10.1 使用
此教程是以 idea专业版 为教程,请自行教程破解版
下载地址:https://tomcat.apache.org/download-10.cgi
直接新建项目,选择 Jakara EE,模板为“Web 应用程序”,接下来就直接下一步,创建。
在图中目录创建 “HelloWorldServlet.java” ,代码如下:
package com.example.servlet;
import jakarta.servlet.*;
import jakarta.servlet.annotation.WebServlet;
import java.io.*;
@WebServlet(name = "HelloWorldServlet", value = "/HelloWorldServlet")
public class HelloWorldServlet extends GenericServlet {
public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException {
// 得到输出流 PrinterWriter 对象,Servlet 使用输出流来产生响应
PrintWriter out = response.getWriter();
// 使用输出流对象向客户端发送字符数据
out.println("Hello World");
}
}
点击右上角的三角形就可以运行项目了
在浏览器中打开 http://localhost:8080/chapter03/cn/itcast/firstapp/servlet/HelloWorldServlet
效果图如下所示
将 “HelloWorldServlet.java” 中的代码修改成下面:
package com.example.servlet;
import jakarta.servlet.*;
import jakarta.servlet.annotation.WebServlet;
@WebServlet(name = "HelloWorldServlet", value = "/HelloWorldServlet")
public class HelloWorldServlet extends GenericServlet {
public void init(ServletConfig config) throws ServletException {
System.out.println("init methed is called");
}
public void service(ServletRequest request, ServletResponse response) throws ServletException {
System.out.println("Hello World");
}
public void destroy() {
System.out.println("destroy method is called");
}
}
重新运行程序
重新运行程序并在浏览器中打开 http://localhost:8080/chapter03/HelloWorldServlet
可以看到终端,这边是我们需要的截图(只需要截终端的那部分)
示意图:
分别在图中的两个路径中创建 “RequestMethodServlet.java” 和 “form.html”
RequestMethodServlet.java 文件中的代码如下:
package com.example.servlet;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.*;
@WebServlet(name = "RequestMethodServlet", value = "/RequestMethodServlet")
public class RequestMethodServlet extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response) throws IOException {
PrintWriter out = response.getWriter();
out.write("this is doGet method");
}
public void doPost(HttpServletRequest request,
HttpServletResponse response) throws IOException {
PrintWriter out = response.getWriter();
out.write("this is doPost method");
}
}
form.html 文件中的代码如下:
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Titletitle>
head>
<body>
<form action="/chapter03/RequestMethodServlet" method="post">
姓名<input type="text" name="name"/><br/>
密码<input type="text" name="psw"/><br/>
<input type="submit" value="提交"/>
form>
body>
html>
重新运行程序并在浏览器中打开 http://localhost:8080/chapter03/form.html 截图,点击“提交”后截图
在如图文件夹中创建 “TestServlet02.java”
package com.example.servlet;
import jakarta.servlet.ServletConfig;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebInitParam;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
@WebServlet(name = "TestServlet02", value = "/TestServlet02",
initParams = {@WebInitParam(name = "encoding", value = "UTF-8")})
public class TestServlet02 extends HttpServlet {
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
PrintWriter out = response.getWriter();
// 获得 ServletConfig 对象
ServletConfig config = this.getServletConfig();
// 获得参数名为 encoding 对应的参数值
String param = config.getInitParameter("encoding");
out.println("encoding=" + param);
}
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
this.doGet(request, response);
}
}
重新运行程序并在浏览器中打开 http://localhost:8080/chapter03/TestServlet02 并截图
在如图路径中新建文件 “TestServlet03.java”
package com.example.servlet;
import jakarta.servlet.ServletContext;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;
@WebServlet("/TestServlet03")
public class TestServlet03 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html; charset=utf-8");
PrintWriter out = response.getWriter();
// 得到 ServletContext 对象
ServletContext context = this.getServletContext();
// 得到包含所有初始化参数名的Enumeration 对象
Enumeration<String> paramNames = context.getInitParameterNames();
out.println("all the paramName and paramValue are following: ");
// 遍历所有的初始化参数名,得到相应的参数值并打印
while (paramNames.hasMoreElements()) {
String name = paramNames.nextElement();
String value = context.getInitParameter(name);
out.println(name + ": " + value);
out.println("
");
}
}
public void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
this.doGet(request, response);
}
}
在如图文件 “web.xml” 中添加如图部分代码
<context-param>
<param-name>companyName</param-name>
<param-value>itcast</param-value>
</context-param>
<context-param>
<param-name>address</param-name>
<param-value>beijing</param-value>
</context-param>
重新运行程序并在浏览器中打开 http://localhost:8080/chapter03/TestServlet03 并截图
在如下目录中创建文件 TestServlet04.java 和 TestServlet05.java
TestServlet04.java 文件内容如下
package com.example.servlet;
import jakarta.servlet.ServletContext;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet("/TestServlet04")
public class TestServlet04 extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
ServletContext context = this.getServletContext();
// 通过 setAttribute()方法设置属性值
context.setAttribute("data", "this servlet save data");
}
public void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
this.doGet(request, response);
}
}
TestServlet05.java 文件内容如下
package com.example.servlet;
import jakarta.servlet.ServletContext;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
@WebServlet("/TestServlet05")
public class TestServlet05 extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
PrintWriter out = response.getWriter();
ServletContext context = this.getServletContext();
// 通过 getAttribute()方法获取属性值
String data = (String) context.getAttribute("data");
out.println(data);
}
public void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
this.doGet(request, response);
}
}
重新运行项目并在浏览器中打开 http://localhost:8080/chapter03/TestServlet04 ,然后打开 http://localhost:8080/chapter03/TestServlet05 并截图
在如下目录中创建文件 TestServlet06.java
package com.example.servlet;
import jakarta.servlet.ServletContext;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Properties;
@WebServlet("/TestServlet06")
public class TestServlet06 extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html; charset=utf-8");
ServletContext context = this.getServletContext();
PrintWriter out = response.getWriter();
//获取相对路径中的输入流对象
InputStream in = context
.getResourceAsStream("/itcast.properties");
Properties pros = new Properties();
pros.load(in);
out.println("Company=" + pros.getProperty("Company") + "
");
out.println("Address=" + pros.getProperty("Address") + "
");
}
public void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
this.doGet(request, response);
}
}
重新运行项目并在浏览器中打开 http://localhost:8080/chapter03/TestServlet06 并截图
恭喜! 该实训到此结束!!!