上一节,我们使用myeclipse部署了web项目,但那部署的为静态的web项目,下面我们来学习编写动态的web项目,编写动态项目必须要用到的为:servlet.

  Servlet是由sun公司命名的,Servlet = Server + Applet(Applet表示小应用程序),Servlet是在服务器端运行的小程序。

编写一个现实登陆的Servlet,并部署访问.

1.编写登录的jsp页面,采用post方式提交。

访问此JSP页面:

2.编写LoginServlet 来通过后台校验登录的用户名和密码。

  1. package com.njupt.demo;
  2.  
  3. import java.io.IOException;
  4. import java.io.PrintWriter;
  5.  
  6. import javax.servlet.ServletException;
  7. import javax.servlet.http.HttpServlet;
  8. import javax.servlet.http.HttpServletRequest;
  9. import javax.servlet.http.HttpServletResponse;
  10.  
  11. public class LoginServlet extends HttpServlet {
  12.  
  13. /**
  14. * Constructor of the object.
  15. */
  16. public LoginServlet() {
  17. super();
  18. }
  19.  
  20. /**
  21. * Destruction of the servlet. <br>
  22. */
  23. public void destroy() {
  24. super.destroy(); // Just puts "destroy" string in log
  25. // Put your code here
  26. }
  27.  
  28. /**
  29. * The doGet method of the servlet. <br>
  30. *
  31. * This method is called when a form has its tag value method equals to get.
  32. *
  33. * @param request the request send by the client to the server
  34. * @param response the response send by the server to the client
  35. * @throws ServletException if an error occurred
  36. * @throws IOException if an error occurred
  37. */
  38. public void doGet(HttpServletRequest request, HttpServletResponse response)
  39. throws ServletException, IOException {
  40.  
  41. response.setContentType("text/html");
  42. PrintWriter out = response.getWriter();
  43. out
  44. .println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
  45. out.println("<HTML>");
  46. out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
  47. out.println(" <BODY>");
  48. out.print(" This is ");
  49. out.print(this.getClass());
  50. out.println(", using the GET method");
  51. out.println(" </BODY>");
  52. out.println("</HTML>");
  53. out.flush();
  54. out.close();
  55. }
  56.  
  57. /**
  58. * The doPost method of the servlet. <br>
  59. *
  60. * This method is called when a form has its tag value method equals to post.
  61. *
  62. * @param request the request send by the client to the server
  63. * @param response the response send by the server to the client
  64. * @throws ServletException if an error occurred
  65. * @throws IOException if an error occurred
  66. */
  67. public void doPost(HttpServletRequest request, HttpServletResponse response)
  68. throws ServletException, IOException {
  69.  
  70. response.setContentType("text/html");
  71. PrintWriter out = response.getWriter();
  72. out
  73. .println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
  74. out.println("<HTML>");
  75. out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
  76. out.println(" <BODY>");
  77. out.print(" This is ");
  78. out.print(this.getClass());
  79. out.println(", using the POST method");
  80. out.println(" </BODY>");
  81. out.println("</HTML>");
  82. out.flush();
  83. out.close();
  84. }
  85.  
  86. /**
  87. * Initialization of the servlet. <br>
  88. *
  89. * @throws ServletException if an error occurs
  90. */
  91. public void init() throws ServletException {
  92. // Put your code here
  93. }
  94.  
  95. }

上图为我们使用myeclipse 建立Servlet的时候生成的,我们需要重点关注的为doget 和 dopost 的方法,决定了采用的为get 方式还是post 方式;另外当我们创建完Servlet后,我观察web.xml 发现:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app version="2.4"
  3. xmlns="http://java.sun.com/xml/ns/j2ee"
  4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5. xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
  6. http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
  7. <servlet>
  8. <description>This is the description of my J2EE component</description>
  9. <display-name>This is the display name of my J2EE component</display-name>
  10. <servlet-name>LoginServlet</servlet-name>
  11. <servlet-class>com.njupt.demo.LoginServlet</servlet-class>
  12. </servlet>
  13.  
  14. <servlet-mapping>
  15. <servlet-name>LoginServlet</servlet-name>
  16. <url-pattern>/servlet/LoginServlet</url-pattern>
  17. </servlet-mapping>
  18. <welcome-file-list>
  19. <welcome-file>index.jsp</welcome-file>
  20. </welcome-file-list>
  21. </web-app>

会进行Servlet 注册和 Servlet 的映射,这也是我们前面讲到的。

下面我进行LoginServlet的编写,改写dopost方法,和编写index.jsp中action 目标和提交方式。

  1. package com.njupt.demo;
  2.  
  3. import java.io.IOException;
  4. import java.io.PrintWriter;
  5.  
  6. import javax.servlet.ServletException;
  7. import javax.servlet.http.HttpServlet;
  8. import javax.servlet.http.HttpServletRequest;
  9. import javax.servlet.http.HttpServletResponse;
  10.  
  11. public class LoginServlet extends HttpServlet {
  12.  
  13. /**
  14. * Constructor of the object.
  15. */
  16. public LoginServlet() {
  17. super();
  18. }
  19.  
  20. /**
  21. * Destruction of the servlet. <br>
  22. */
  23. public void destroy() {
  24. super.destroy(); // Just puts "destroy" string in log
  25. // Put your code here
  26. }
  27.  
  28. /**
  29. * The doGet method of the servlet. <br>
  30. *
  31. * This method is called when a form has its tag value method equals to get.
  32. *
  33. * @param request the request send by the client to the server
  34. * @param response the response send by the server to the client
  35. * @throws ServletException if an error occurred
  36. * @throws IOException if an error occurred
  37. */
  38. public void doGet(HttpServletRequest request, HttpServletResponse response)
  39. throws ServletException, IOException {
  40.  
  41. doPost(request,response);
  42. }
  43.  
  44. /**
  45. * The doPost method of the servlet. <br>
  46. *
  47. * This method is called when a form has its tag value method equals to post.
  48. *
  49. * @param request the request send by the client to the server
  50. * @param response the response send by the server to the client
  51. * @throws ServletException if an error occurred
  52. * @throws IOException if an error occurred
  53. */
  54. public void doPost(HttpServletRequest request, HttpServletResponse response)
  55. throws ServletException, IOException {
  56.  
  57. String username = request.getParameter("username");
  58. String password = request.getParameter("password");
  59.  
  60. response.setContentType("text/html");
  61. PrintWriter out = response.getWriter();
  62. if("test".equals(username) && "123456".equals(password))
  63. {
  64. out
  65. .println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
  66. out.println("<HTML>");
  67. out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
  68. out.println(" <BODY>");
  69. out.print(" sucess ");
  70. out.println(" </BODY>");
  71. out.println("</HTML>");
  72. }
  73. else
  74. {
  75. out
  76. .println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
  77. out.println("<HTML>");
  78. out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
  79. out.println(" <BODY>");
  80. out.print(" error ");
  81. out.println(" </BODY>");
  82. out.println("</HTML>");
  83. }
  84. out.flush();
  85. out.close();
  86. }
  87.  
  88. /**
  89. * Initialization of the servlet. <br>
  90. *
  91. * @throws ServletException if an error occurs
  92. */
  93. public void init() throws ServletException {
  94. // Put your code here
  95. }
  96.  
  97. }

即:只有输入正确的用户名和密码,将会显示sucess;否则输出error.

部署项目

结果如下:

输入正确则:

本章结束。

http://blog.csdn.net/jiuqiyuliang/article/details/36424981

Myeclipse 搭建Java Web 项目:Servlet 《二》的更多相关文章

  1. 使用MyEclipse搭建java Web项目开发

    转自:http://blog.csdn.net/jiuqiyuliang/article/details/36875217 首先,在开始搭建MyEclipse的开发环境之前,还有三步工具的安装需要完成 ...

  2. 【java项目实战】一步步教你使用MyEclipse搭建java Web项目开发环境(一)

    首先.在開始搭建MyEclipse的开发环境之前.还有三步工具的安装须要完毕,仅仅要在安装配置成功之后才干够进入以下的java Web项目开发环境的搭建. 1.安装工具 第一步,下载并安装JDK,到官 ...

  3. Myeclipse 搭建Java Web 项目 《一》

    今天将图文并茂的介绍如何使用myclipse 创建Java Web 项目;我使用的是myclipse 8.6 来进行创建: 1.打开Myeclipse,点击File --->然后New ---- ...

  4. 使用Intellij idea新建Java Web项目(servlet) 原理及初步使用

    准备 JDK       (配置JDK_HOME\bin   和 CLASSPATH)   注:JDK8下载已经需要注册了,请使用JDK11(现在是官方长期支持的版本)     对于我们新手来说,JD ...

  5. MyEclipse开发Java Web项目步骤

    1.安装工具 第一步,下载安装JDK,并配置环境: 配置环境变量步骤: (1)新建变量名:JAVA_HOME,变量值为JDK的安装路径: (2)打开PATH,添加变量值:%JAVA_HOME%\bin ...

  6. Docker学习笔记之搭建 Java Web 项目运行环境

    0x00 概述 Java Web 泛指以 Java 程序为基础向外提供 Web 服务的技术及相关工具,狭义上来说,我们也可以说 Java Web 是由 Servlet 程序提供的 Web 服务. 对我 ...

  7. Intellij Idea搭建java web项目(问题总结)

    这两天突发奇想下载了Intellij Idea,准备体验下这个传说中很强大IDE.工具下载就不多说了,网上一搜便知,博主是直接从Intellij官网下载的最新完整版,可惜的是只能使用30天,不过也差不 ...

  8. Eclipse使用Maven搭建Java Web项目,并直接部署Tomcat

    1.环境: win10 Java 1.8 Maven 3.3.9 Eclipse IDE for Java EE Developers 2.准备: eclipse环境什么的不赘述,Maven环境还是要 ...

  9. Eclipse使用Maven搭建Java Web项目,并直接部署Tomcat(转载)

    原文地址:http://www.cnblogs.com/hackyo/p/6527910.html 1.环境: win10 Java 1.8 Maven 3.3.9 Eclipse IDE for J ...

随机推荐

  1. Android与JNI(二) ---- Java调用C++ 动态调用

    目录: 1. 简介 2. JNI 组件的入口函数 3. 使用 registerNativeMethods 方法 4. 测试 5. JNI 帮助方法 6. 参考资料 1. 简介 Android与JNI( ...

  2. 数据库 Mysql事务详解

    Mysql事务 mysql的事务默认是自动提交的,也就是你提交一个query,他就直接执行!我们可以通过 禁止自动提交 开启自动提交 //mysql事务 #include <stdio.h> ...

  3. JQuery基础知识(2)

    JQuery基础知识(2) JQuery滑动效果 1. JQuery slideDown(); 语法: $(selector).slideDown(speed,callback); 可选的 speed ...

  4. 初识Google code jam平台

    为了熟悉一下code jam的平台,今天简单试了一下,做了一下Qualification Round Africa 2010的三道题目,都是很基础的. A题:给一个数n和一系列数a[],从a[]中找出 ...

  5. numpy 安装

    sudo apt-get install python-scipy sudo apt-get install python-numpy sudo apt-get install python-matp ...

  6. AndroidManifest.xml文件

    AndroidManifest.xml常用标签解读 1.全局篇(包名,版本信息) 2.组件篇(四大组件) Activity Service Content Provider Broadcast Rec ...

  7. UVa 10706 - Number Sequence

    题目大意:Sk表示从1到k的字符序列,如S4为1234,现如今有如下的序列S1S2...Sk,形如1 12 123 1234这样的序列,给一个数n,让你去这个序列第n个位置上的数字. 可以构建出一个S ...

  8. iOS 之 UITextView

    _lableAssess = [[UITextView alloc] init]; [_lableAssess setFrame:CGRectMake(left2, top2, width2, siz ...

  9. Windows Server 2008 R2防火墙出站规则

    出战规则指Windows Server 2008 R2系统访问外部的某台计算机通信数据流. 配置防火墙阻止Windows Server 2008 R2系统通过IE软件访问外部的网站服务器,阻止Wind ...

  10. Canvas drawImage API

    drawImage <!DOCTYPE html> <html lang="en"> <head> <meta charset=" ...