Use IDEA to create a Servlet application

Use IDEA to create a Servlet application

Step 1: Create a web application

Select the web application application, then complete the project name and so on.

Step Two: Project Configuration

Create a directory under WEB-INF two folders: classes and lib, where classes are JAVA Servlet class and other classes must be stored location, type the following directory structure reflects the class package. lib is required Servlet application JAR files to be deployed here. But the Servlet API JAR file does not need to deploy here, the container has been its backup.

2.1 modify the class file output directory

Select the menu File ---> Project Structure .. ---> select modules ----> path option to output class file is changed classes directory created earlier

2.2 modify the storage jar file

Or in the modules menu, select the Dependencies tab ---> Click to the right of the "+", select "JARs or directories ...", choose to create a lib directory

Lib directory to the directory we created in the WEB-INF, selected as follows:

The third step: write Servlet program

In the src directory, create a servlet file, which reads as follows:

package app01a;

import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(name = "MyServlet",urlPatterns = {"/my"})

public class MyServlet implements Servlet{
    private transient ServletConfig servletConfig;
    @Override
    public void init(ServletConfig servletConfig) throws ServletException {
        this.servletConfig = servletConfig;
    }

    @Override
    public ServletConfig getServletConfig() {
        return servletConfig;
    }

    @Override
    public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException {
        String servletName = servletConfig.getServletName();
        //网页响应类型,浏览器将其渲染为HTML格式
        response.setContentType("text/html");
        PrintWriter writer = response.getWriter();
        writer.println("<html><head></head>" + "<body> Hello from " + servletName + "</body></html>");
    }

    @Override
    public String getServletInfo() {
        return "My Servlet";
    }

    @Override
    public void destroy() {

    }
}

Step Four: Configure Tomcat

Select Edit Configurations ..

Here we must note the path of the JRE, do not select the default, select the path you want to install the JRE, I appeared in the case of the 1099 port is occupied in the running, but with netstat -ano | findstr 1099 and did not find the task PID, after Baidu said JRE version must be strictly consistent path.

Meanwhile, URL modify the access path under the Deployment tab.

Reference: IDEA configuration of Tomcat Servlet Web project creation and simple operation

Step Five: Run the program

Access localhost: 8080 / application name / servlet name

Problems encountered

When you start TomCat, log garbled

百度了各种博客,修改了一大堆还是继续乱码,偶然间因为1099端口占用,去tomcat/bin目录下启动tomcat时,发现win10的命令行下也是乱码情况,参考的博客,完美解决了问题:

解决最新版tomcat在window10下启动可能出现启动日志乱码的情况

关于web.xml的错误

在第四步配置Tomcat后,我们访问这个ServletTest的路径变成了http://localhost:8080/my,这样访问的路径是WEB-INF下的index.jsp文件,在web.xml中配置了servlet-mapping后,才能访问对应的servlet对象

 <servlet>
        <servlet-name>MyServlet</servlet-name>
        <servlet-class>app01a.MyServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>MyServlet</servlet-name>
        <url-pattern>/test</url-pattern>
    </servlet-mapping>

访问http://localhost:8080/my/test可以访问对应的servlet对象,调用service方法,浏览器应该渲染出service方法中的HTML内容。但是访问时出现了错误 错误代码500

中午睡了一觉起来,没问题了。。。。。先记录一下别的错误,如果下次再遇到,再记录叭(」><)」

Servlet使用注解

使用注解WebServlet时,刚开始只是取代了web.xml中的 标签,运行程序后404页面,参考博客: Servlet使用注解配置URl提示404错误

在web.xml的 标签中使用属性metadate-complete="false"之后即可运行成功。

<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_4_0.xsd"
         version="4.0"
         metadata-complete ="true">

原因:

Servlet 3.0 的部署描述文件 web.xml 的顶层标签 有一个 metadata-complete 属性,该属性指定当前的部署描述文件是否是完全的。如果设置为 true,则容器在部署时将只依赖部署描述文件,忽略所有的注解(同时也会跳过 web-fragment.xml 的扫描,亦即禁用可插性支持;如果不配置该属性,或者将其设置为 false,则表示启用注解支持(和可插性支持)。

至此,一个简单:(的Servlet应用终于调通辽。。。。

Guess you like

Origin www.cnblogs.com/zz-1226/p/11229616.html