Get ready:
1. Installing jdk1.7
2. Installing tomcat1.8
First, create and set up the Javaweb project
1. Create a Javaweb project
File----New---Project ...
Set Project name:
After the creation is complete, the project structure is as follows:
2. Web Engineering Settings
2.1 Right-click in the Web-inf directory, New---Directory, create classes and lib two directories
The classes directory is used to store the compiled class file, and Lib is used to store dependent jar packages
2.2 File--and Project Structure ..., go to Project Structure window, click Modules-Select Item "Javaweb"-Switch to Paths tab--> ; Tick "Use module compile output path" to change "Output path" and "Test output path" to the classes directory created previously
The class file that will be compiled by default is generated to the classes directory
2.3 Click Modules--- Select item "javaweb"-Switch to Dependencies tab-click "+" on the right, select "JARs or Directories ..." and select the Lib directory to create
Select Jar Directory
2.4 Configuration Packaging method artifacts: Click on the Artifacts tab, idea will automatically create a package named "Javaweb:war exploded" for the project, which means that it is packaged into a war package and is file-expandable. The output path is the Out folder under the current project and remains the default. Also tick "Build on Make", indicating that the compile time package deployment, tick "show content of elements", indicating the display of a detailed list of content.
3. Tomcat Configuration
3.1 Run, Edit configurations, go to the "Run Configurations" window, click "+", "Tomcat Server," Local, create a new Tomcat container
3.2 Enter a new service name at "name", click "Configure ..." after "Application Server", eject the Tomcat server window, select the locally installed Tomcat directory, OK
3.3 in the " Server" palette of the "Run Configurations" window, uncheck "After launch", set "HTTP port" and "JMX port", click Apply, This completes the Tomcat configuration.
4. Javaweb Test
4.1 Run, Edit configurations, go to the "Run Configurations" window, select the previously configured Tomcat, click on the "Deployment" tab, and click "+" Artifact , select the artifact of the Web project you created ...
Modify "Application Context", Apply and OK
Description: The application context here refers to the root directory of the project
4.2 Add the content that you want to display between the body in the index.jsp file, and then click "Run" in the green triangle
Open Browser, type: Localhost:8080/javaweb
At this point, IntelliJ idea creates and sets up the Javaweb project complete, where the servlet is written and run.
Second, the servlet simple implementation
1. Writing the servlet source file
Create a new Helloworld.java in the SRC directory and write the code and compile it:
[Java]View PlainCopy
- Import javax.servlet.ServletException;
- Import Javax.servlet.http.HttpServlet;
- Import Javax.servlet.http.HttpServletRequest;
- Import Javax.servlet.http.HttpServletResponse;
- Import java.io.IOException;
- Import Java.io.PrintWriter;
- Public class HelloWorld extends HttpServlet {
- Private String message;
- @Override
- public void Init () throws servletexception {
- Message = "Hello World, this message was from servlet!";
- }
- @Override
- protected void Doget (HttpServletRequest req, HttpServletResponse resp) throws Servletexception, IOException {
- //Set response content type
- Resp.setcontenttype ("text/html");
- //Set logical implementation
- PrintWriter out = Resp.getwriter ();
- Out.println (""
- }
- @Override
- public Void Destroy () {
- Super.destroy ();
- }
- }
After compiling, you will find that the Helloworld.class file is generated in the classes directory
2. Deploying the Servlet
Method One:
Add the following to the <web-app> tag of the Web. xml file in the Web-inf directory:
[Plain]View PlainCopy
- <servlet>
- <servlet-name>HelloWorld</servlet-name>
- <servlet-class>HelloWorld</servlet-class>
- </servlet>
- <servlet-mapping>
- <servlet-name>HelloWorld</servlet-name>
- <url-pattern>/HelloWorld</url-pattern>
- </servlet-mapping>
Method Two:
Precede the class of the HelloWorld file with: @WebServlet ("/helloworld")
3. Run the servlet
Click the Run button
Console appears successfully the Tomcat service started successfully! Open Browser input: Localhost:8080/javaweb/helloworld to see the servlet running status.
(Original address: http://blog.csdn.net/yhao2014/article/details/45740111)
Go Intellij idea Create javaweb and servlet simple implementation