package com.xieyuan; import java.io.IOException; import java.io.PrintWriter; import java.net.URLEncoder; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.sun.corba.se.impl.javax.rmi.CORBA.Util; public class MyServlet extends HttpServlet { /** * Constructor of the object. */ public MyServlet() { 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> * * 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 { execute(request, response); } /** * The doPost method of the servlet. <br> * * 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 { execute(request, response); } private void execute(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException { //设置编码模式 request.setCharacterEncoding("UTF-8"); response.setCharacterEncoding("UTF-8"); //获取请求的URL String requestUrl=request.getRequestURI(); //获得请求的方式 String method=request.getMethod(); //获得请求的参数 String param=request.getParameter("name"); //设置文档类型 response.setContentType("text/html"); PrintWriter out=response.getWriter(); out.println("<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">"); out.println("<HTML>"); out.println("<HEAD><TITLE>我的第一个Servlet页面</TITLE></HEAD>"); out.println("<BODY>"); out.println("尊敬的"+(param==null? "用户":param)+",您使用了"+method+"方法,访问了"+requestUrl+"页面"); out.println("<form action='"+requestUrl+"' method='get' >"); out.println("<input type='text' name='name' /><input type='submit' value='GET提交' />"); out.println("</form><BR/>"); out.println("<form action='"+requestUrl+"' method='post' >"); out.println("<input type='text' name='name' /><input type='submit' value='POST提交' />"); out.println("</form><BR/>"); out.println("<BR/>您的IP:"+request.getRemoteAddr()+",服务器信息:"+this.getServletContext().getServerInfo()+ ",上次访问页面为:"+request.getHeader("referer")+"<BR/><script>document.write('本页面最后更新时间:'+document.lastModified);</script>"); 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 { } }
JAVA WEB开发环境搭建,请看:http://blog.csdn.net/tabactivity/article/details/11097783