欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

浅谈IDEA实用的Servlet模板

程序员文章站 2022-03-03 09:41:29
一、前言不会再idea中创建模板点击这里看教程二、这是模板内容,直接创建自己的模板复制用即可#if (${package_name} && ${package_name} != "")...

一、前言

不会再idea中创建模板点击这里看教程

二、这是模板内容,直接创建自己的模板复制用即可

#if (${package_name} && ${package_name} != "")package ${package_name};#end
#parse("file header.java")
#set( $packagename1 = "#if (${package_name} && ${package_name} != '')${package_name}.#end#parse('file header.java')" )
#set( $packagename2 = "#if (${package_name} && ${package_name} != '')${package_name}/#end#parse('file header.java')" )
 
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.ioexception;
import java.io.printwriter;
/**
 * @auther liuweirui
 * @date ${date} ${time}
 */
 //更改@webservlet中value的值,可以修改访问该servlet文件的名称,规范value = "/visit name"
@webservlet(name = "${name}", value = "/${name}")
public class ${name} extends httpservlet {
 
    /**
     * constructor of the object.
     */
    public ${name}() {
        super();
    }
 
    /**
     * destruction of the servlet. <br>
     */
    public void destroy() {
        super.destroy(); // just puts "destroy" string in log
        // put your code here
    }
 
    /**
     * the doget method of the servlet. <br>
     * <p>
     * this method is called when a form has its tag value method equals to get.
     *
     * @param request  the request send by the client to the server
     * @param response the response send by the server to the client
     * @throws servletexception if an error occurred
     * @throws ioexception      if an error occurred
     */
    public void doget(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception {
        dopost(request, response);
    }
 
    /**
     * the dopost method of the servlet. <br>
     * <p>
     * this method is called when a form has its tag value method equals to post.
     *
     * @param request  the request send by the client to the server
     * @param response the response send by the server to the client
     * @throws servletexception if an error occurred
     * @throws ioexception      if an error occurred
     */
    public void dopost(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception {
        //设置请求和响应数据的编码
        request.setcharacterencoding("utf-8");
        response.setcharacterencoding("utf-8");
        response.setcontenttype("text/html;charset=utf-8");
        printwriter out = response.getwriter();
        //页面内容
        out.println("<!doctype html public \"-//w3c//dtd html 4.01 transitional//en\">");
        out.println("<html>");
        out.println("  <head><title>a servlet</title></head>");
        out.println("  <body>");
        out.print("    this is ");
        out.print(this.getclass());
        out.println(", using the post method");
        out.println("  </body>");
        out.println("</html>");
        out.flush();
        out.close();
    }
 
    /**
     * initialization of the servlet. <br>
     *
     * @throws servletexception if an error occurs
     */
    public void init() throws servletexception {
        // put your code here
    }
    
    /**
    * web.xml配置文件书写
    * <servlet>
    *   <servlet-name>${name}</servlet-name>
    *   <servlet-class>$packagename1${name}</servlet-class>
    * </servlet>
    
    * <servlet-mapping>
    *   <servlet-name>${name}</servlet-name>
    *   <url-pattern>/$packagename2${name}</url-pattern>
    * </servlet-mapping>
    */
}

三、优点

1.设置好的请求和响应数据的编码

浅谈IDEA实用的Servlet模板

2.处理好的doget()和dopost()方法

浅谈IDEA实用的Servlet模板

3.记录日期和创建者(创建者自己改,这里我用的本人的)

浅谈IDEA实用的Servlet模板

4.导入好的包和设置好的包名

浅谈IDEA实用的Servlet模板

5.设置好的@webservlet属性,设置@webservlet属性可以替代配置web.xml

浅谈IDEA实用的Servlet模板

6.设置好的配置web.xml文件的内容,复制好即可用

浅谈IDEA实用的Servlet模板

四、问题

web配置文件的内容有些问题,如图:

这是正常情况,在包下创建文件

浅谈IDEA实用的Servlet模板

这是缺省状态下创建的文件

浅谈IDEA实用的Servlet模板

<servlet-class>的内容在缺省状态下会出现package_name.,这是以下这段代码导致的问题

#if (${package_name} && ${package_name} != '')${package_name}.#end#parse('file header.java')

这段代码貌似不能出现在注释内容里,不然就会出问题,在包中创建正常显示,但在缺省状态下就会显示package_name

使用前还需自己修改

到此这篇关于浅谈idea实用的servlet模板的文章就介绍到这了,更多相关实用的servlet模板内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!