# JJU_干干
Tomcat配置
1. 下载:https://tomcat.apache.org/
2. 打开eclipse,在windows选项 / Server / Runtime Environment / Add ,添加下载后解压好的的Tomcat文件夹
方法一:.java + .xml
1. 创建一个Java Web 项目
(1)选中 project:
(2)创建动态web项目:
(3)点击next,命名项目名;再连续点击next,勾选生成xml文件选项
--------------------------------------------------------------------------------------------------------
2. 代码:java+xml
.java:
package case_01; 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 HelloWorld extends HttpServlet{ public void service(HttpServletRequest req, HttpServletResponse res) throws IOException,ServletException{ PrintWriter out = res.getWriter(); out.print("Hellow World!"); } }
.xml:
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"> <display-name>Servlet_01</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <servlet> <servlet-name>hello</servlet-name> <!--包名.类名--> <servlet-class>case_01.HelloWorld</servlet-class> </servlet> <servlet-mapping> <servlet-name>hello</servlet-name> <!--这里的servlet-name一定要和上面的一样--> <url-pattern>/hello</url-pattern> <!-- 这里一定要加左斜杠,且这里的名字可以任意,不是非得是hello,但习惯保持一样 --> </servlet-mapping> </web-app>
xml位置:
-------------------------------------------------------------------------------------
3. 运行:
- 在 .java中鼠标右键,选择Run as 中的 Run on Server
- 选择Tomcat容器,点击finish
4. 运行结果:
方法二:使用注解方式配置Sevrlet:直接创建一个servlet项目,自动生成一个模板
代码:
package case_01; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Servlet implementation class HelloWorld_02 */ @WebServlet("/HelloWorld_02") public class HelloWorld_02 extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public HelloWorld_02() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub // 以下两条语句防止输入中文出现乱码 request.setCharacterEncoding("utf-8"); response.setContentType("text/html;charset = utf-8"); response.getWriter().append("Served at: ").append(request.getContextPath()); PrintWriter out = response.getWriter(); out.print("Hellow World! I'm 干干"); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }
运行结果: