index.jsp页面代码%@pagelanguage=javacontentType=text/html;charset=utf-8pageEncoding=utf-8%!DOCTYPEhtmlPUBLIC-//W3C//DTDHTML4.01Transitional//EN=Content-Typecontent=text/html;charset=utf-8titleServlet生成动态信息示例/title/headbodyformid=form1name=form1method=postaction=Dynamiclabel请输入您的名字:/labelinputtype=textname=userName/pinputtype=submitname=Submitvalue=提交//p/form/body/htmlDynamic.java代码packagecom.cc;importjava.io.IOException;importjava.io.PrintWriter;importjavax.servlet.ServletException;importjavax.servlet.annotation.WebServlet;importjavax.servlet.http.HttpServlet;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;/***ServletimplementationclassDynamic*/@WebServlet(/Dynamic)publicclassDynamicextendsHttpServlet{privatestaticfinallongserialVersionUID=1L;/***@seeHttpServlet#HttpServlet()*/publicDynamic(){super();//TODOAuto-generatedconstructorstub}/***@seeHttpServlet#doGet(HttpServletRequestrequest,HttpServletResponseresponse)*/protectedvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{//TODOAuto-generatedmethodstub//response.setContentType(text/html;charset=gbk);request.setCharacterEncoding(utf-8);response.setCharacterEncoding(utf-8);PrintWriterout=response.getWriter();out.println(htmlheadmetahttp-equiv='Content-Type'content='text/html;charset=utf-8'titleServlet生成动态网页/title/headbodyp);out.print(非法的请求);out.println(/p/body/html);}/***@seeHttpServlet#doPost(HttpServletRequestrequest,HttpServletResponseresponse)*/protectedvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{//TODOAuto-generatedmethodstubrequest.setCharacterEncoding(utf-8);StringuserName=request.getParameter(userName);Stringip=request.getRemoteAddr();//response.setContentType(text/html;charset=gbk);request.setCharacterEncoding(utf-8);response.setCharacterEncoding(utf-8);PrintWriterout=response.getWriter();out.println(htmlheadmetahttp-equiv='Content-Type'content='text/html;charset=utf-8');out.println(titleServlet生成动态网页/title);out.println(/headbodyp);out.print(欢迎strong);out.print(userName);out.print(/strong来自strong);out.print(ip);out.print(/strong);out.println(的朋友/p/body/html);}}