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

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

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

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

访问此JSP页面:

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

package com.njupt.demo;

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 {

    /**
     * Constructor of the object.
     */
    public LoginServlet() {
        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 {

        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();
    }

    /**
     * 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();
    }

    /**
     * Initialization of the servlet. <br>
     *
     * @throws ServletException if an error occurs
     */
    public void init() throws ServletException {
        // Put your code here
    }

}

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

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
    xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
    http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
  <servlet>
    <description>This is the description of my J2EE component</description>
    <display-name>This is the display name of my J2EE component</display-name>
    <servlet-name>LoginServlet</servlet-name>
    <servlet-class>com.njupt.demo.LoginServlet</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>LoginServlet</servlet-name>
    <url-pattern>/servlet/LoginServlet</url-pattern>
  </servlet-mapping>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

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

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

package com.njupt.demo;

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 {

    /**
     * Constructor of the object.
     */
    public LoginServlet() {
        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 {

        doPost(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 {

        String username = request.getParameter("username");
        String password = request.getParameter("password");

        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        if("test".equals(username) && "123456".equals(password))
        {
            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("  sucess  ");
            out.println("  </BODY>");
            out.println("</HTML>");
        }
        else
        {
            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("  error  ");
            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 {
        // Put your code here
    }

}

即:只有输入正确的用户名和密码,将会显示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(三) ---- c++调用java(转载)

    源码下载:JniDemo JNI就是Java Native Interface, 即可以实现Java调用本地库, 也可以实现C/C++调用Java代码, 从而实现了两种语言的互通, 可以让我们更加灵活 ...

  2. Java 汉子转拼音

    1. 引入: pinyin4j-1.1.0 包;  http://pan.baidu.com/s/1eQ8a874 2. 转换; private static String ChineseToPiny ...

  3. java web面试

    1. session和cookie的区别和联系,session的生命周期,多个服务部署时session管理. 原博http://blog.csdn.net/shuaishenkkk/article/d ...

  4. eclipse hibernate plugin

    JBoss Tools hibernate tools for eclipse plugins

  5. LeetCode---Depth-first && Breadth-first

    417. Pacific Atlantic Water Flow 思路:构造两个二维数组分别存储大西洋和太平洋的结果,先初始化边界,然后从边界出发,深度优先遍历,标记满足条件的所有节点 static ...

  6. DDD设计一个电商网站

    DDD设计一个电商网站(十一)-- 最后的准备    阅读目录 前言 准备 实现 结语 一.前言 最近实在太忙,上周停更了一周.按流程一步一步走到现在,到达了整个下单流程的最后一公里--结算页的处理. ...

  7. Ionic在线打包IOS平台应用

    参见:http://docs.ionic.io/services/profiles/#ios-app-certificate--provisioning-profile Ionic云编译,需要注册.地 ...

  8. U盘为什么还有剩余空间,但却提示说空间不够

    你的U盘是FAT32格式,它只支持单一小于4G的文件复制,将U盘改为NTFS格式,可以解决题.方法:开始——运行,输入“cmd”,回车,在命令符后输入:convert h: /fs:ntfs,回车(假 ...

  9. HTML 颜色值

    HTML 颜色值 颜色由红(R).绿(G).蓝(B)组成. 颜色值 颜色值由十六进制来表示红.绿.蓝(RGB). 每个颜色的最低值为0(十六进制为00),最高值为255(十六进制为FF). 十六进制值 ...

  10. HTTP的GET方法模拟

    进行GET方法的测试 #telnet[ ]10.1.1.11[ ]80 GET[ ]/[ ]HTTP/1.0 [两个回车] HEAD[]/[]HTTP/1.0[回车回车] http://www.cnb ...