Table of contents
1. Create a new java project, File->New->Project->enter the project name javaweb_first->Create
3. Create a new WEB-INF directory
5. Copy the contents of the web.xml file
7. Project structure configuration
10. Project configuration Tomcat
Use IDE (IDEA)
The whole process is full of pictures and texts, teaching you step by step from a J2SE project to creating a JAVAWEB project
Xiaobai records use
1. Create a new java project, File->New->Project->enter the project name javaweb_first->Create
2. Create a new web directory
Right-click the project root directory (javaweb_first) -> New->Directory-> enter web->ok
3. Create a new WEB-INF directory
Right-click the web directory, create a new WEB-INF directory, and the operation is the same as above
4. New web.xml
Right-click the WEB-INF directory, create a new File, and enter web.xml
5. Copy the contents of the web.xml file
Go to the tomcat directory /examples/WEB-INF/web.xml in the tomcat installation directory
Open web.xml and copy all the content to the web.xml we just created
leave only node content
Note that due to the different versions of tomcat, the content of web.xml is also different, so don't read my content.
6. Create new index.html
Right click on the web directory -> New -> HTML File
Write whatever you want in the newly created html file!
7. Project structure configuration
Click on File->Project Structure
8. Select Facets (features)
On the Project Structure page, click Facets->select javaweb_first->click OK
Configure the web.xml path for deployment
Compare whether the file path is consistent with our project
9. Configure Artifacts
On the Project Structue page, click Atifacts->Select Web Application: Exploded->Empty
10. Project configuration Tomcat
In the drop-down box next to the run button, click Edit Configuration->click the plus sign->select Tomcat Server->Local->
11. Configure Deployment
13. Click to run
14. Introduce servlet-api.jar
- Copy the servlet-api.jar under the lib folder in the tomcat installation directory
- Create a new lib folder in the web directory of the project
- Paste servlet-api.jar to the lib folder
- Right-click the lib folder and click Add as Libraries
15. Create HelloServlet in the src directory
public class HelloServlet implements Servlet { @Override public void init(ServletConfig servletConfig) throws ServletException { System.out.println("HelloServlet init called..........我只被调用一次"); } @Override public ServletConfig getServletConfig() { return null; } @Override public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException { PrintWriter writer = servletResponse.getWriter() ; writer.println("Hello Servlet!!!!!!!!!!!!!"); writer.flush(); } @Override public String getServletInfo() { return null; } @Override public void destroy() { System.out.println("HelloServlet destroy called..........我只被调用一次"); } }
16. Configure HelloServlet under web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1" metadata-complete="true"> <servlet> <servlet-name>hello</servlet-name> <servlet-class>com.gggl.javaweb.HelloServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>hello</servlet-name> <url-pattern>/hello</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>hello</servlet-name> <url-pattern>/hello2</url-pattern> </servlet-mapping> </web-app>
17. Rerun, enter http://localhost:8080/javaweb_first/hello in the browser
Modify Error:
1. TomEE required to support EAR/EJB deployment error
After configuring tomcat just now, it may report Error: TomEE required to support EAR/EJB deployment.
1. Check if you configured Artifacts to select J2ee exploded. If so, please refer to step 9 to select Web Application: Exploded->Empty
2. Refer to the error report Configuration Error: deployment source 'xxx:war' is not valid
2. 404 after starting the web page, check the Facets under the project structure directory to see if it is associated with Artifacts.