您的位置:首页 > 编程语言

[技巧篇]01.Servlet的优化模版代码

2015-06-10 19:58 183 查看
Servlet.java的模版

#---------------------------------------------#
# <aw:description>Template for Servlet</aw:description>
# <aw:version>1.1</aw:version>
# <aw:date>04/05/2003</aw:date>
# <aw:author>Ferret Renaud</aw:author>
#---------------------------------------------#

<aw:import>java.io.IOException</aw:import>
<aw:import>java.io.PrintWriter</aw:import>

<aw:import>javax.servlet.ServletException</aw:import>
<aw:import>javax.servlet.http.HttpServlet</aw:import>
<aw:import>javax.servlet.http.HttpServletRequest</aw:import>
<aw:import>javax.servlet.http.HttpServletResponse</aw:import>

<aw:parentClass>javax.servlet.http.HttpServlet</aw:parentClass>

<aw:constructor name="c1">
/**
* Constructor of the object.
*/
public <aw:className/>() {
super();
}

</aw:constructor>

<aw:method name="doGet">
/**
* 处理Get请求的方法
* 一般用于:跳转到添加,跳转到更新和删除操作,一般用于传递主键
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

//设置响应内容的类型和编码
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();

//捕获异常使用
try {
//运行JS脚本
//out.write("<script type='text/javascript'></script>");

//解决路径问题,标准写法
//请求转发--服务端请求
//request.getRequestDispatcher("/文件或者文件夹").forward(request, response);
//重定向--客户端请求
//response.sendRedirect(request.getContextPath()+"/文件或者文件夹");

} catch (Exception e) {
e.printStackTrace();
}
}

</aw:method>

<aw:method name="doPost">
/**
* 处理Post请求的编码
* 一般用于:保存数据更新数据
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//设置POST请求编码
request.setCharacterEncoding("UTF-8");

//设置响应内容的类型和编码
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();

//捕获异常使用
try {
//运行JS脚本
//out.write("<script type='text/javascript'></script>");

//解决路径问题,标准写法
//请求转发--服务端请求
//request.getRequestDispatcher("/文件或者文件夹").forward(request, response);
//重定向--客户端请求
//response.sendRedirect(request.getContextPath()+"/文件或者文件夹");

} catch (Exception e) {
e.printStackTrace();
}

}

</aw:method>

<aw:method name="doPut">
/**
* The doPut method of the servlet. <br>
*
* This method is called when a HTTP put request is received.
*
* @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 doPut(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

// Put your code here
}

</aw:method>

<aw:method name="doDelete">
/**
* The doDelete method of the servlet. <br>
*
* This method is called when a HTTP delete request is received.
*
* @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 doDelete(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

// Put your code here
}

</aw:method>

<aw:method name="init">
/**
* Servlet鲁玫脢录禄炉:
*        脰禄脢脟脭脷碌脷脪禄麓脦碌梅脫脙碌脛脢卤潞貌鲁玫脢录禄炉脪禄麓脦
*/
public void init() throws ServletException {

}

</aw:method>

<aw:method name="destroy">
/**
* Servlet脧煤禄脵:
*        脰禄脢脟脭脷路镁脦帽脝梅鹿脴卤脮碌脛脢卤潞貌脧煤禄脵
*/
public void destroy() {

}

</aw:method>

<aw:method name="getServletInfo">
/**
* Returns information about the servlet, such as
* author, version, and copyright.
*
* @return String information about this servlet
*/
public String getServletInfo() {
return "This is my default servlet created by Eclipse";
}

</aw:method>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: