src 目录下com.xieyuan包MyServlet.java文件(Servlet文件)
package com.xieyuan; import java.awt.Color; import java.awt.Font; import java.awt.Graphics2D; import java.awt.image.BufferedImage; import java.io.IOException; import java.io.PrintWriter; import java.net.URLEncoder; import java.util.Enumeration; import java.util.Random; import javax.servlet.ServletException; import javax.servlet.ServletOutputStream; 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; import com.sun.image.codec.jpeg.JPEGCodec; import com.sun.image.codec.jpeg.JPEGImageEncoder; 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"); response.setContentType("text/html"); String getName=request.getParameter("name"); String getHibby=request.getParameter("hobby"); PrintWriter out=response.getWriter(); out.println("<DOCTYPE HTML PUBLIC "-//W3C//DTD HTML4.0 Transitional//EN">"); out.println("<HTML>"); out.println("<HEAD><TITLE>结果</TITLE></HEAD>"); out.println("<BODY>"); out.println("您的信息如下:"); out.println(getName+" "+getHibby); out.println("<br/><input type='button' value='返回上一页' onclick='history.go(-1)' />"); out.println("</BODY>"); out.println("</HTML>"); } /** * Initialization of the servlet. <br> * * @throws ServletException if an error occurs */ public void init() throws ServletException { } }部署好WEB后,访问:http://127.0.0.1:8080/Test/servlet/MyServlet 进入登录页面
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'index.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <form action="servlet/MyServlet" method="post"> <div align="center"><BR/> <fieldset style='80%' > <legend>填写信息</legend><br/> <div class="line" align="left"> 姓名:<input type="text" name="name" width="200px" /><br/> 爱好:<input type="checkbox" name="hobby" value="踢球" id="tiqiu"/> <label for="tiqiu">踢球</label> <input type="checkbox" name="hobby" value="上网" id="shangwang" /> <label for="shangwang">上网</label> <input type="checkbox" name="hobby" value="下棋" id="xiaqi" /> <label for="xiaqi">下棋</label> </div> <br/><br/><br/><br/><input type="submit" value=" 提 交 "> </fieldset> </div> </form> <form>