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 (this 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:

package yanguoqi;
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

打开web.xml,坑爹的是JCreator貌似打不开,一打开就报错要求关闭。不得已,用eclipse打开来编辑。

<?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>


第四步:输入网址访问

到tomcat文件夹下,找到bin下得startup打开,然后输入:http://localhost:8080/myWebSite/yanguoqi进行访。

注:reload一个servlet的方法:输入网址:http://localhost:8080/,点TomcatManager。会要求输入用户名和密码。我的是解压缩版本的,因此要自己设置下。如果是安装版的会提示设置密码。找到conf文件夹下的tomcat-users,添加用户及密码:

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

至此完毕!明日再战。



Guess you like

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