Servlet代码为:
配置文件为:
基本就是这些,部署之后,运行结果见附件图片!
package com.babyDuncan.Servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class firstServlet extends HttpServlet {
@Override
protected long getLastModified(HttpServletRequest req) {
// TODO Auto-generated method stub
this.log("执行getLastModified方法");
return -1;
}
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.log("执行doGet方法!");
doExecete(request, response);
}
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.log("执行doPost方法!");
doExecete(request, response);
}
private void doExecete(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
// 设置request和response的编码
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
// 获取request的URI
String requestURI = request.getRequestURI();
// 获取request的方法,是post还是get
String requestMethod = request.getMethod();
// 获取request的参数
String param = request.getParameter("param");
//设置返回的数据类型
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html><head><title>babyDuncan 's Servelt</title></head><body>");
out.println("以" + requestMethod + "方法访问页面,取到的参数为" + param );
out.println("<form action='" +requestURI+ "' method = 'get' >" +
"<input type='text' name='param' value='input the param!'/>" +
"<input type='submit' value='以get方式访问页面'"+requestURI+ "/></form>" );
out.println("<form action='" +requestURI+ "' method = 'post' >" +
"<input type='text' name='param' value='input the param!'/>" +
"<input type='submit' value='以post方式访问页面'"+requestURI+ "/></form>" );
out.println("<script>document.write('本页最后的访问时间是'+ document.lastModified);</script>");
out.println("</body>");
out.println("</html>");
out.flush();
out.close();
}
}
配置文件为:
<servlet>
<servlet-name>firstServlet</servlet-name>
<servlet-class>com.babyDuncan.Servlet.firstServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>firstServlet</servlet-name>
<url-pattern>/servlet/firstServlet.jsp</url-pattern>
<url-pattern>/wocao.ppp</url-pattern>
</servlet-mapping>
基本就是这些,部署之后,运行结果见附件图片!