Java web development -------- Servlet learning of HelloWorld from deployment to development of the whole process

Today, I finally came up with the HelloWorld of Servlet and recorded the process.

The following figure is an overall frame diagram:

Development flow chart:

Step 1: Configure TOMCAT

In the installation directory of tomcat, find the webapps folder, create a new folder myWebSite (the name can be arbitrary, representing your own project name), create a new WEB-INF under the myWebSite folder (the name cannot be wrong), and in the WEB-INF file Create two new folders under the folder, the classes and lib folders, and create a new file web.xml. Here you can find the ROOT folder under the webapps folder, and copy the WEB-INF folder inside to myWebSite. Folders that do not have to be created.

Step 2: Develop Servlet (introduce servlet-api.jar)

The tool used here is JCreator, in fact, Eclipse can also. The thing to do in this step is to create a new Hello class in the classes folder above. First download and install JCreator, create a new JavaFile, name Hello, and select the path to the classes folder. Then introduce the servlet-api.jar package. Click JCreator's configuration---option---JDK configuration file, select the JDK version on the right, as shown in the figure above, click Edit, click Add--Add Archive, and then put the servlet- under the lib folder in the TOMCAT installation directory. api.jar is selected and OK.

Write code:

Bao Yanguo;
import javax.servlet.*;
import java.io.*;

public class hello {

} Then click Tools---Implement Interface---find javax---servlet---servlet and select it.

The final Hello.java source code is:

//这是我的第一个servlet,使用实现servlet接口的方式来开发
package yanguoqi;
import javax.servlet.*;
import java.io.*;
import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import java.io.IOException;

public class Hello implements Servlet{
	/**
	 * Method init
	 *
	 *
	 * @param parm1
	 *
	 @throws ServletException
	 *
	 */
	 
	 //初始化servlet,类似于构造函数
	 //只第一次访问survlet时被调用
	public void init(ServletConfig parm1) throws ServletException {
		// TODO: 在这添加你的代码
		System.out.println("init ");
	}

	/**
	 * Method getServletConfig
	 *
	 *
	 * @return
	 *
	 */
	 
	 //得到servlet配置文件,不太重要
	public ServletConfig getServletConfig() {
		// TODO: 在这添加你的代码
		return null;
	}

	/**
	 * Method service
	 *
	 *
	 * @param parm1
	 * @param parm2
	 *
	 @throws ServletException
	 @throws IOException
	 *
	 */
	 
	 //用于处理业务逻辑
	public void service(ServletRequest parm1, ServletResponse res) throws ServletException, IOException {
		// TODO: 在这添加你的代码
		System.out.println("service it");
		PrintWriter pw = res.getWriter();	
		pw.println("Hello World!");
		
	}

	/**
	 * Method getServletInfo
	 *
	 *
	 * @return
	 *
	 */
	public String getServletInfo() {
		// TODO: 在这添加你的代码
		return "";
	}

	/**
	 * Method destroy
	 *
	 *
	 */
	public void destroy() {
		// TODO: 在这添加你的代码
		System.out.println("destroy!");
	}
	
}

Note that this program needs to be compiled after it is written! It should also compile after each modification.

Step 3: Deploy web.xml

Open web.xml, the pit father is that JCreator seems to be unable to open, and an error is reported to be closed as soon as it is opened. As a last resort, use eclipse to open to edit.

<?xml version="1.0" encoding="UTF-8"?> 
<!-- ISO-8859-1 -->
<!-- Licensed to the Apache Software Foundation (ASF) under one or more contributor 
	license agreements. See the NOTICE file distributed with this work for additional 
	information regarding copyright ownership. The ASF licenses this file to 
	You under the Apache License, Version 2.0 (the "License"); you may not use 
	this file except in compliance with the License. You may obtain a copy of 
	the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required 
	by applicable law or agreed to in writing, software distributed under the 
	License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS 
	OF ANY KIND, either express or implied. See the License for the specific 
	language governing permissions and limitations under the License. -->

<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	version="2.5">

	<display-name>Welcome to Tomcat</display-name>
	<description>
     Welcome to Tomcat
  </description>

	<servlet>
		<!--给survlet起个名字,可以是任意的 -->
		<servlet-name>hello</servlet-name>
		<!--servlet的路径(包名+类名)-->
		<servlet-class>yanguoqi.Hello</servlet-class>
	</servlet>

	<servlet-mapping>
		<servlet-name>hello</servlet-name>
		<!-- 这是在浏览器中输入的访问该survlet的url,任意的 -->
		<url-pattern>/yanguoqi</url-pattern>

	</servlet-mapping>

</web-app>


Step 4: Enter the URL to visit

Go to the tomcat folder, find the bin and open the startup, and then enter: http://localhost:8080/myWebSite/yanguoqi to visit.

Note: The method of reloading a servlet: enter the URL: http://localhost:8080/ , and click TomcatManager . You will be asked to enter a username and password. Mine is the decompressed version, so I have to set it myself. If it is an installed version, you will be prompted to set a password. Find tomcat-users in the conf folder and add the user and password:

<role rolename="manager-gui"/>
<user username="yan" password="yan" roles="manager-gui"/>

That's it! Fight again tomorrow.



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324155041&siteId=291194637