第一个servlet

昨天看SSM的配置文件感觉很困难,跟超哥谈了一会儿感觉领悟了不少了,看来还是得从servlet去了解服务器的配置问题。无论如何还是先把项目跑起来,再去深入理解问题。

servlet与CGI都是服务器端的小程序,但是它们又有很多不同。

  • servlet与CGI的区别
    servlet是线程级别,多线程的(但是不是线程安全),而CGI是进程级别的。
    servlet是依靠JVM的,可以实现java跨平台的特性。
    CGI是web服务器和数据库服务器两层架构,servlet是有数据库连接池,可以进行对数据库访问的事务管理。
    servlet由java容器管理,它实现了java自带的HttpServlet接口,里面有三个方法。分别是int(),service(),destroy(),servlet的构造方法在init之前执行。在客户端请求发出,容器就会自动调用int的方法进行初始化,然后可以多次调用service方法,在最后调用一次destroy方法。

  • jsp也是一个变种的java,在运行过后jsp会编译到相应路径下的java文件和class文件。

  • 第一个servlet.
    这里写图片描述

    新建一个Dynmantic web project。建立文件路径如下:
    这里写图片描述

package servletPackage;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class LoginServlet
 */
@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public LoginServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        this.doPost(request, response);
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        String firstName = request.getParameter("firstName");
        String lastName = request.getParameter("lastName");
        System.out.println(firstName + " " + lastName);
    }

}

为调用servlet的dopost方法,从request请求中取出firstName与lastName参数并在console控制台输出。
然后我们来编写一个获取firstName与lastName的jsp页面:
就在默认的index.jsp页面中编写:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="UTF-8"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>

<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>首页</title>
</head>
<body>
<form action = "kkk/login" id = "form1"  method = "post">
 firstName:<input type = "text" name = "firstName" value = "micro" />   //kkk/login对应web.xml中的servlet执行
 <br/>
 lastName:<input type = "text" name = "lastName" value = "xiaoma" />
 <br/>
 <input type = "submit" name = "" value = "提交" />
 </form>
</body>
</html>

然后我们就要将index.jsp提交数据进行处理,web.xml配置文件为:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
<servlet>  //这里配置一个servlet与它的实现类
<servlet-name>login</servlet-name>
<servlet-class>servletPackage.LoginServlet</servlet-class>
</servlet>
<servlet-mapping>  //这里配置servlet与对应url
<servlet-name>login</servlet-name>
<url-pattern>/kkk/login</url-pattern>
</servlet-mapping>

<welcome-file-list><welcome-file>index.jsp</welcome-file></welcome-file-list>
</web-app>

如何编第一个servlet
简单的servlet例子

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值