一个Servlet的实例
2016-04-23 21:17
239 查看
需求:在一个表单中输入姓名和年龄,点击提交,然后打印出来。
1.新建一个WebProject.
2.在WebRoot目录下新建一个html文件(这里取名为myHtml.html).
3.在src 目录上点击右键,新建一个Servlet。Servelt 名为LoginServlet.包名为myServletTest.
4.编辑WebRoot中的Web.xml 文件。
5.编辑LoginServlet.java文件。
6.启动服务器。
7.访问
1.新建一个WebProject.
2.在WebRoot目录下新建一个html文件(这里取名为myHtml.html).
<!DOCTYPE html> <html> <head> <title>登录</title> </head> <body> <form action="loginServlet" method="get"> 姓名:<input type="text" name="name" style="width:150px" /><p/> 年龄:<input type="text" name="age" style="width:150px" /><p> <input type="submit" value="提交" /> </form> </body> </html>
3.在src 目录上点击右键,新建一个Servlet。Servelt 名为LoginServlet.包名为myServletTest.
4.编辑WebRoot中的Web.xml 文件。
5.编辑LoginServlet.java文件。
package myServletTest; 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 loginServlet extends HttpServlet { /** * 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 { // 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>A Servlet</TITLE></HEAD>"); // out.println(" <BODY>"); // out.print(" This is "); // out.print(this.getClass()); // out.println(", using the GET method"); // out.println(" </BODY>"); // out.println("</HTML>"); // out.flush(); // out.close(); String name = request.getParameter("name"); String age = request.getParameter("age"); PrintWriter out = response.getWriter(); out.println("name =" + name); out.println("age=" + age); out.flush(); out.close(); } /** * 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 { 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>A Servlet</TITLE></HEAD>"); out.println(" <BODY>"); out.print(" This is "); out.print(this.getClass()); out.println(", using the POST method"); out.println(" </BODY>"); out.println("</HTML>"); out.flush(); out.close(); } }
6.启动服务器。
7.访问
相关文章推荐
- 1026 Sudoku Killer
- 引导页中ViewPage的使用
- GitHub 里的笔记
- ArcObjects c++ 获取栅格或矢量图层的范围
- 欢迎页实现
- Java-Struts2:Can not access a member of class with modifiers "public"
- Navicat 提示Cannot create oci environment 解决方案
- 剑指offer-从尾到头打印链表
- HDU 5191 Building Blocks
- 站立会议个人博客5(2016/4/23)
- 学习笔记:使用 OpenCV 识别 QRCode 转载自why的blog
- 项目自定义Preferences
- 创建struts2支持的第一个应用(二)
- 通过RAII机制实现资源的自动释放
- php基本符号大全
- 第一阶段冲刺个人博客06
- 时间日期工具类(相关转化)
- Android 日志工具类log
- Android自定义Toast
- 用MyEclipse编验证码(1)