Servlet实例代码
2017-09-28 本文已影响0人
_String_
测试代码
package servlet1;
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 servlet_test extends HttpServlet {
String message;
//构造函数
public servlet_test() {
super();
}
public void init() throws ServletException{
message = "this is message test";
}
//get方法请求
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println(message);
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
}
public void destroy(){
//nouthon
}
}
测试效果如图:
运行结果