Build first javaweb project by Intellj idea

2017-04-06

#Download the Intellj idea

Clicking the title and jumping to the Official Website of Intellj idea . In this site , you can download the dmg file for Intellj idea . At this step you will find this software is not free . Don’t worry , find poll code at here.

#Prepare the server enviroument

Click the title , do as my previous blog .

#Build a javaweb project

Start your Intellj idea , you will see a interface like the figure downstairs.

Select the “Plugins” , just like next figure .

Find the choice about Tomcat server , select it and apply .

#Creat a new javaweb project

Creat two directoys , “lib” and “classes” .

The next is a part from Official doc .

/WEB-INF/classes/ - This directory contains any Java class files (and associated resources) required for your application, including both servlet and non-servlet classes, that are not combined into JAR files. If your classes are organized into Java packages, you must reflect this in the directory hierarchy under /WEB-INF/classes/. For example, a Java class named com.mycompany.mypackage.MyServlet would need to be stored in a file named /WEB-INF/classes/com/mycompany/mypackage/MyServlet.class.

/WEB-INF/lib/ - This directory contains JAR files that contain Java class files (and associated resources) required for your application, such as third party class libraries or JDBC drivers.

So after build this directories , we should change the setting .

First Step : Open Module Settings .

Second Step : Set the out path to “classes” directory.

Third Step : Set the lib path to “lib” directory.

Fourth Step : Put the jar file we need to lib . You can find it in the Tomcat doc which you download when you build tomcat server.

Run hello.java

Build a class under src named “HelloWorld”. Put the next code at this class.

  import javax.servlet.annotation.WebServlet;
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;
@WebServlet("/HelloWorld")
public class HelloWorld extends HttpServlet {
    private String message;

    @Override
    public void init() throws ServletException {
        message = "Hello world, this message is from servlet!";
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //设置响应内容类型
        resp.setContentType("text/html");

        //设置逻辑实现
        PrintWriter out = resp.getWriter();
        out.println("<h3>" + message + "</h3>");
    }

    @Override
    public void destroy() {
        super.destroy();
    }
}

Then set the “Run Configurations”. Just the pictures downstairs.

then run it . And Click here

点击查看评论

所有文章