Java server-side development-servlet: 1_2_2, how to develop a servlet, write a servlet in practice

Article Directory

 3. Develop the first Servlet in the MyEclipse tool

Step1, start myeclipse, let myeclipse configure and manage tomcat

step2, build a web project

(1) Write a HelloServlet class

(2) Configure web.xml

(3) Find Servers in the myEclipse print console and start the tomact container

(4) Deploy the web project

(5) Check the running effect

4. Precautions

(1) Automatically generate Servers file

(2)报错:The ResourceConfig instance does not contain any root resource classes


 3. Develop the first Servlet

Step1, start myeclipse, let myeclipse configure and manage tomcat

The premise is that you have done these three blog posts:

MyEclipse2017 installation: MyEclipse2017 installation method (Mac)

Tomact installation: Apache Tomact installation and configuration steps detailed (Mac)

MyEclipse2017 configuration tomact: MyEclipse configuration apache Tomact steps detailed (Mac)

step2, build a web project

If you are creating a new web project, please refer to the above example "Create a web project".

Here directly in the previous example, create a new package name class name

(1) Write a HelloServlet class

package t02_写第一个Servlet;

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;

/**
 * 写一个简单的Servlet
 * @UpdateTime:2011年02月28日 下午17:30:00
 * @ProjectName: web01
 * @ClassName:HelloServlet
 * @CategoryName:java类
 * @author:luminal、邮箱 [email protected]
 * @since 1.0
 * @Description:(可以在这里描述这个类的作用)
 * 1、照着写代码,体会、解释执行过程
 * 2、继承HttpServlet、写service方法【固定格式,先照着写】
 * 3、Servlet配置、访问地址,在WebRoot/WEB-INF/web.xml文件里面
 * 4、Servlet执行过程介绍【见:博文 或 Doc( 文档 )】
 */
public class HelloServlet extends HttpServlet{
		protected void service(HttpServletRequest request, HttpServletResponse response) 
		throws ServletException, IOException {
			//step1:输出处理结果
			//通过response,设置一个消息头(content-type),
			//告诉浏览器,服务器返回的数据类型
			response.setContentType("text/html");
			
			//step2:通过response(响应对象)获得一个输出流
			PrintWriter out = response.getWriter();
			
			//step3:将处理结果数据缓存到response对象上
			//-->处理结果,打包发送给浏览器
			//-->浏览器拆包,生成页面【详见Servlet执行过程介绍】
			out.println("Hello Servlet");
//			out.println("<div style='font-size:90px;font-style:italic;color:red;'>Hello Servlet</div>");
			
			//关闭流
			out.close();
			
		}
}

(2) Configure web.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" 
version="3.1">

<display-name>web01</display-name>

<!--   
  <servlet>
    <description>JAX-RS Tools Generated - Do not modify</description>
    <servlet-name>JAX-RS Servlet</servlet-name>
    <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>JAX-RS Servlet</servlet-name>
    <url-pattern>/jaxrs/*</url-pattern>
  </servlet-mapping> -->
  
  
  <!--
		<servlet>
		<servlet-name>自定义名</servlet-name>
		<servlet-class>包名.类名</servlet-class>
		</servlet>
		<servlet-mapping>
		<servlet-name>自定义名</servlet-name>
		<url-pattern>/自定义名</url-pattern>
		</servlet-mapping>

//  访问地址:
//  http://localhost:8080/项目名/ 【WebRoot\WEB-INF\web.xml里面的<url-pattern>” 】对应的名字
//  http://localhost:8080/web01/hello
	-->
	
		<servlet>
		<servlet-name>xxx</servlet-name>
		<!-- 包名里面有汉字和下划线,不会报错 -->
		<servlet-class>t02_写第一个Servlet.HelloServlet</servlet-class>
		</servlet>
		<servlet-mapping>
		<servlet-name>xxx</servlet-name>
		<url-pattern>/hello</url-pattern>
		</servlet-mapping>
  
  
 
</web-app>


(3) Find Servers in the myEclipse print console and start the tomact container

         

Or you can start tomact like this

(4) Deploy the web project

Or you can deploy web projects like this

        

finish, ok, after the deployment is complete, check the Console

二月 27, 2020 1:34:51 下午 org.apache.catalina.startup.HostConfig deployDirectory
信息: Deploying web application directory /Users/luminal/Documents/LuminalCode/Luminal_Java/t03_Web_Servlet_JSP_Jquery_Ajax/code/.metadata/.me_tcat85/webapps/web01
二月 27, 2020 1:34:53 下午 org.apache.jasper.servlet.TldScanner scanJars
信息: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
二月 27, 2020 1:34:53 下午 org.apache.catalina.startup.HostConfig deployDirectory

信息: Deployment of web application directory 
/Users/luminal/Documents/LuminalCode/Luminal_Java/t03_Web_Servlet_JSP_Jquery_Ajax/code
/.metadata/.me_tcat85/webapps/web01 
has finished in 1,487 ms

The deployment is successful, we can just look at the last message, which roughly means:

Deploying the web application project to the directory xxxxxxx has been completed, taking 1478 milliseconds 

Deployment fails, usually there will be an error message

(5) Check the running effect

// Access address:
// http://localhost:8080/project name/ [<url-pattern>” in WebRoot\WEB-INF\web.xml] Corresponding name //
http://localhost:8080/ web01/hello

Enter in the local browser: http://localhost:8080/web01/hello

The effect is as follows:

My source code: Java server development (2) write the first Servlet-Java document class resource-CSDN download

4. Precautions

(1) Automatically generate Servers file

myEclipse2017 seems to automatically generate a Servers file in the working directory, I don't care about it,

There is no research, who knows what the hell is welcome to leave a message!

(2)报错:The ResourceConfig instance does not contain any root resource classes

Possible reference: The ResourceConfig instance does not contain any root resource classes

Guess you like

Origin blog.csdn.net/YuDBL/article/details/126576422