Go Intellij idea Create javaweb and servlet simple implementation

Source: Internet
Author: User
Tags tomcat server

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
  1. Import javax.servlet.ServletException;
  2. Import Javax.servlet.http.HttpServlet;
  3. Import Javax.servlet.http.HttpServletRequest;
  4. Import Javax.servlet.http.HttpServletResponse;
  5. Import java.io.IOException;
  6. Import Java.io.PrintWriter;
  7. Public class HelloWorld extends HttpServlet {
  8. Private String message;
  9. @Override
  10. public void Init () throws servletexception {
  11. Message = "Hello World, this message was from servlet!";
  12. }
  13. @Override
  14. protected void Doget (HttpServletRequest req, HttpServletResponse resp) throws Servletexception, IOException {
  15. //Set response content type
  16. Resp.setcontenttype ("text/html");
  17. //Set logical implementation
  18. PrintWriter out = Resp.getwriter ();
  19. Out.println (""
  20. }
  21. @Override
  22. public Void Destroy () {
  23. Super.destroy ();
  24. }
  25. }


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
    1. <servlet>
    2. <servlet-name>HelloWorld</servlet-name>
    3. <servlet-class>HelloWorld</servlet-class>
    4. </servlet>
    5. <servlet-mapping>
    6. <servlet-name>HelloWorld</servlet-name>
    7. <url-pattern>/HelloWorld</url-pattern>
    8. </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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.