inotgo.com

Download area
File name File size
servlet-api.jar 193k
j2ee.rar 2k
server.xml 6k
Use the webmaster secret download tool

Tool version compatibility problem
Servlet It cannot run independently , Need to be in a web Running in the application
And one web The application is deployed in tomcat In

So develop a servlet The following steps are required
establish web Application project
to write servlet code
Deploy to tomcat In

Developed IDE There are also various options , Some use MyEclipse, Some use eclipse of EE edition , Some use IDEA development .

This example uses Eclipse EE Version combination independent tomcat Have a java Ordinary items The creation of .

The advantage of this is , Create a... In the most primitive way web application , Can master the most basic knowledge .

In the future, when relevant problems make mistakes , More conducive to solving .

notes : Dynamic WEB The style of the project is How to Eclipse Create a dynamic Web project There are also explanations in

Must read : be based on Tomcat For your program to run successfully , There are strict requirements for the correctness of the configuration file , Anything goes wrong , Will cause the operation to fail . If you are learning this tutorial for the first time , Be sure to follow the instructions of the tutorial strictly , Completely imitate the operation , Until you successfully see the running effect . After the first success , Confidence , Ideas will have a better cushion , Then according to their doubts , stay “ success ” Make the desired changes and adjustments to your code , This can greatly save learning time , increase of efficiency , Don't change it at once , Create obstacles to your learning
Correct


14 branch 25 second
This video uses html5 Play mode , If it cannot be played normally , Please upgrade your browser to the latest version , Recommend Firefox ,chrome,360 browser . If thunderbolt is installed , Play the video and show the direct download status , Please adjust Thunderbolt system settings - Basic settings - Start - Monitor all browsers ( Remove this option ). chrome of Video download Plug-in will affect playback , as IDM etc. , Please close or switch other browsers

Step 1 : Run... First , See the effect , Learn again   
Step 2 : Imitation and troubleshooting   
Step 3 : Create a java project j2ee   
Step 4 : Import required servlet-api.jar Bag   
Step 5 : to write HelloServlet   
Step 6 : to configure web.xml   
Step 7 : Specify the project output to classes catalogue   
Step 8 : to configure tomcat of server.xml In <context   
Step 9 : Delete tomcat webapps Next ROOT catalogue   
Step 10 : Restart tomcat, visit http://127.0.0.1/hello   
Step 11 : Complete project   

Step 1 :

Run... First , See the effect , Learn again

edit top fracture
Run... First , Learn again

This knowledge point is a JAVA WEB project , A lot of steps need to be done , Any step is missing , Did something wrong , Can fail , This will affect the confidence of learning , And mistakenly think that this tutorial is impassable .

So first download the runnable project in the upper right corner :j2ee.rar, Unzip and deploy in tomcat In , Observe for normal operation . Make sure you can run , Make sure the tutorial can run , Learn the following .

deploy tomcat Please refer to : stay tomcat Deploy in JAVA WEB application

After successful deployment , Test address

http://127.0.0.1/hello

notes : This tutorial uses 80 port
 Run... First , See the effect , Learn again
Step 2 :

Imitation and troubleshooting

edit top fracture
After ensuring that the runnable project can run correctly , Then strictly follow the steps in the tutorial , Imitate the code again .
The imitation process inevitably has code differences , As a result, the expected operation results cannot be obtained , At this moment, by comparison The correct answer ( Runnable project ) And your own code , To locate the problem .
In this way , Learning is effective , Troubleshooting is efficient , It can obviously improve the learning speed , Across all the barriers on the way to learning .

It is recommended to use diffmerge Software , Compare folders . Put your own project folder , Compare with my runnable project folder .
This software is awesome , You can know which two files in the folder are wrong , And clearly marked
Here is a green installation and use tutorial : diffmerge Download and use tutorials
File->New->Other->Java->Java Project
Enter a name j2ee
This creates a simple based on java My project
be careful : must Put it in e:\project\j2ee Under the directory , Subsequent configuration is based on this directory
be careful : Do not use the following methods
File->New->Other-Web->Dynamic Web Project
This is eclipse ee How to create a project , It will put all kinds of tomcat, The generation of relevant configuration files is automatically done . And these details , That's exactly what you need to learn and master . Therefore, this method is not recommended for beginners .
 Create a java project j2ee
Step 4 :

Import required servlet-api.jar Bag

edit top fracture
Because it's development Servlet So you need to use servlet-api.jar The related classes in this package .

servlet-api.jar The package is located at D:\tomcat\lib\servlet-api.jar

Or download from the right side of this page

Import method :

Right click on the item -> properties -> Java Build Path ->Libraries -> Add External JARs
 Import required servlet-api.jar Bag
establish HelloServlet( stay Eclipse The way to create a common class in : Menu ->File->New->Class)
be careful :eclipse When you create it, you will bring a Package name j2ee, Do not use this package name , Leave the package name blank

HelloServlet Inherited HttpServlet
And provides a doGet method

When testing this class later , Will directly enter the address in the browser

http://127.0.01/hello

The way to submit data by entering an address in the browser is GET ( reference resources GET and POST The difference between )
So it's time to Servlet You need to provide a corresponding doGet method

stay doGet In , Output html
namely Title element h1 and String of the current time
 to write HelloServlet
import java.io.IOException; import java.util.Date; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class HelloServlet extends HttpServlet{ public void doGet(HttpServletRequest request, HttpServletResponse response){ try { response.getWriter().println("<h1>Hello Servlet!</h1>"); response.getWriter().println(new Date().toLocaleString()); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
First, in j2ee Create directory under web
Then create the directory web/WEB-INF
Then in WEB-INF Create... In the directory web.xml

web.xml Provide path and servlet Mapping relationship of
hold /hello This path , Map to HelloServlet On this class

<servlet> Under the label <servlet-name>
With <servlet-mapping> Under the label <servlet-name> It must be the same

<servlet-name> With <servlet-class> It can be different , But for ease of understanding and maintenance , They usually write the same . stick out a mile
 to configure web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app> <servlet> <servlet-name>HelloServlet</servlet-name> <servlet-class>HelloServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>HelloServlet</servlet-name> <url-pattern>/hello</url-pattern> </servlet-mapping> </web-app>
<?xml version="1.0" encoding="UTF-8"?>
<web-app>

	<servlet>
		<servlet-name>HelloServlet</servlet-name>
		<servlet-class>HelloServlet</servlet-class>
	</servlet>

	<servlet-mapping>
		<servlet-name>HelloServlet</servlet-name>
		<url-pattern>/hello</url-pattern>
	</servlet-mapping>

</web-app>
Step 7 :

Specify the project output to classes catalogue

edit top fracture
First, in WEB-INF Create... Under classes catalogue

Put the project's class The file output is from the original j2ee/bin Set to j2ee/web/WEB-INF/classes Next

Step :
Right click the item ->properties->Java Build Path->Source-> In the lower right corner Brower-> The specified location is j2ee/web/WEB-INF/classes.

Why this step ? stay ecilpse Default output in class It's in bin Under the directory , But tomcat After startup , By default , Not going to bin Look for these class File , But to WEB-INF/classes Look in this directory . So through this step of configuration , Make eclipse of class File output to WEB-INF/classes Under the directory , So this is the same as tomcat Compatible with .
 Specify the project output to classes catalogue
Step 8 :

to configure tomcat of server.xml In <context

edit top fracture
Tomcat Please use 80 Port , Pure version tomcat

In order to j2ee/web This directory is deployed to tomcat In , Do the following :

Open tomcat/conf/sever.xml

stay <Host name="localhost" ... Add a sentence below


<Context path="/" docBase="e:\\project\\j2ee\\web" debug="0" reloadable="false" />


path="/" It means directly through http://127.0.0.1/hello You can visit the web page .

If set to


<Context path="/j2ee" docBase="e:\\project\\j2ee\\web" debug="0" reloadable="false" />


Then it means that you need to pass http://127.0.0.1/ j2ee/hello To access


If this step always fails , Please download the modified directly on the right server.xml
<Context path="/" docBase="e:\\project\\j2ee\\web" debug="0" reloadable="false" />
<Context path="/" docBase="e:\\project\\j2ee\\web" debug="0" reloadable="false" />
Step 9 :

Delete tomcat webapps Next ROOT catalogue

edit top fracture
Deploy... In the previous step web When I was server.xml In path Configure as "/" With webapps Next ROOT Directory conflict

So you have to Delete ROOT catalogue , If not, you don't have to delete
 Delete tomcat webapps Next ROOT catalogue
Step 10 :

Restart tomcat, visit http://127.0.0.1/hello

edit top fracture
close tomcat after , Restart startup.bat Visit the address below to see servlet Output of

http://127.0.0.1/hello


And every refresh , Can see the output of the new time
 Restart tomcat, visit http://127.0.0.1/hello
last , The complete compressed package of the project is provided on the right .

I really can't figure it out , You can unzip it first , Copy exactly what's inside , You can always do it


The official account of programming , Follow and get the latest tutorials and promotions in real time , thank you .


Q & A area    
2021-09-27 In the previous step tomcat The configuration can also display servlet, stay eclipse Wrote code to show 404
hecat




In the previous step tomcat The configuration can also display servlet, stay eclipse Wrote code to show 404, A name is defined when creating the package ser, stay <servlet-class>ser.HelloServlet</servlet-class> It says , Log error reporting
 Loading
<?xml version="1.0" encoding="UTF-8"?>
<web-app>

	<servlet>
		<servlet-name>HelloServlet</servlet-name>
		<servlet-class>ser.HelloServlet</servlet-class>
	</servlet>

	<servlet-mapping>
		<servlet-name>HelloServlet</servlet-name>
		<url-pattern>hello</url-pattern>
	</servlet-mapping>

</web-app>
package ser;
import java.io.IOException;
import java.util.Date;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class HelloServlet extends HttpServlet{

	public void doGet(HttpServletRequest request, HttpServletResponse response){
		
		try {
			response.getWriter().println("<h1>Hello Servlet!</h1>");
			response.getWriter().println(new Date().toLocaleString());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
}
 
               


2 One answer

One study
Answer time :2021-11-04
He used his eclipse In j2ee web The location of , In the configuration, you need to use your position .

hecat
Answer time :2021-09-27
27-Sep-2021 12:09:54.781 Information [http-nio-80-exec-1] org.apache.catalina.core.ApplicationContext.log Will servlet[HelloServlet] Mark as unavailable 27-Sep-2021 12:09:54.782 serious [http-nio-80-exec-1] org.apache.catalina.core.StandardWrapperValve.invoke Allocation exception servlet [HelloServlet] java.lang.UnsupportedClassVersionError: ser/HelloServlet has been compiled by a more recent version of the Java Runtime (class file version 60.0), this version of the Java Runtime only recognizes class file versions up to 52.0 ( Unable to load . class [ser.HelloServlet]) The error log is



The answer has been submitted successfully , Auditing . Please My answer Check the answer record at , thank you
answer Or code please Fill in at least one , If you have a problem , Please ask again , Otherwise, the webmaster may not see





2021-08-11 HTTP Status 500 – Internal Server Error How to solve
lys123




How to solve this
HTTP Status 500 – Internal Server Error
Type Exception Report

Message Error instantiating servlet class [com.ys.servlet.HelloServlet]

Description The server encountered an unexpected condition that prevented it from fulfilling the request.

Exception

javax.servlet.ServletException: Error instantiating servlet class [com.ys.servlet.HelloServlet]
	org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:490)
	org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
	org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:668)
	org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
	org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:408)
	org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
	org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:791)
	org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1417)
	org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
	java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
	java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
	org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
	java.base/java.lang.Thread.run(Thread.java:835)
Root Cause
 
               





The answer has been submitted successfully , Auditing . Please My answer Check the answer record at , thank you
answer Or code please Fill in at least one , If you have a problem , Please ask again , Otherwise, the webmaster may not see





2021-08-02 Open web page , The date is garbled
2021-06-03 stay IDEA I can't make it in the , Ask the boss for guidance
2021-05-30 stationmaster Can you make a IDEA Tutorial eclipse I've run out of talent


Too many questions , Page rendering is too slow , To speed up rendering , Only a few questions are displayed on this page at most . also 140 Previous questions , please Click to view

Please... Before asking questions land
The question has been submitted successfully , Auditing . Please My question Check the question record at , thank you
about J2EE-Servlet- development servlet Your questions

Try to provide Screenshot code and Abnormal information , Help to analyze and solve problems . You can also enter this station QQ Group communication : 496725845
Ask questions and try to provide complete code , Environment description , The more conducive to the recurrence of the problem , The faster your question can be answered .
Have questions about the code in the tutorial , Please provide which step , Which line is in doubt , This makes it easy to quickly locate the problem , Improve the speed at which questions are answered
In the thousands of questions that already exist , A considerable proportion , Because of the use of and webmaster Different versions of the development environment Resulting in , For example jdk, eclpise, idea, mysql,tomcat Wait, the version of the software is inconsistent .
Please use the same version as the webmaster , You can save yourself a lot of learning time . The webmaster sorted out the software versions used in teaching , It's all here , Convenient for everyone to download : /k/helloworld/helloworld-version/1718.html

Upload screenshot