firstServlet 写一个最基本的servlet程序

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>

基本就是这些,部署之后,运行结果见附件图片!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值