servlet详细及方法介绍

Servlet

Servlet是一种服务端端的Java应用程序,具有独立于平台和协议的特性,可以生成动态的Web页面。 它担当客户请求(Web浏览器或其他HTTP客户程序)与服务器响应(HTTP服务器上的数据库或应用程序)的中间层。 Servlet是位于Web 服务器内部的服务器端的Java应用程序,与传统的从命令行启动的Java应用程序不同,Servlet由Web服务器进行加载,该Web服务器必须包含支持Servlet的Java虚拟机。

访问我的网页示例 上海驾校

Servlet端代码

要实现一个Servlet,首先必须继承HttpServlet。上诉代码实现了客户端请求的get方法处理。需要提醒的是,在浏览器地址栏中通过回车的形式,默认也是get方法
public class PrintWriter extends Writer向文本输出流打印对象的格式化表示形式。此类实现在 PrintStream 中的所有 print 方法。它不包含用于写入原始字节的方法,对于这些字节,程序应该使用未编码的字节流进行写入。

package com.study.servlet;

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 HelloWordServlet extends HttpServlet {
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		resp.setContentType("text/html");
		PrintWriter out = resp.getWriter();
		out.write("<html><head><title>helloworld</title></head>");
		out.write("<body><h>hello world</h></body></html>");
		out.flush();
	}
}  

web.xml代码

在web.xml中,你需要设定<servlet>(可以看做是物理路劲和名称),<servlet-mapping>(可以看成是逻辑路劲)

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 
	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_3_0.xsd">
  	<servlet>
		<servlet-name>helloworld</servlet-name>
		<servlet-class>com.study.servlet.HelloWordServlet</servlet-class>
	</servlet>

	<servlet-mapping>
		<servlet-name>helloworld</servlet-name>
		<url-pattern>/helloworld</url-pattern>
	</servlet-mapping>
</web-app>

jsp表单请求代码

在该jsp中我定义了一个表单,使用from中的action,针对服务器HelloWordServlet 进行get请求

<%@ 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 'HelloWorld.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>
		This is my JSP page.
		<%=new Date().toLocaleString()%><br>

		<form action="/temp/HelloWordServlet">
			username <input type="text" name = "username"><br>
			password <input type="password" name = "password"><br>
			<input type="submit" value="提交">   
			<input type="reset" value="reset">
		</form>

	</body>
</html>

action="/temp/HelloWordServlet" 也可以携程action="HelloWordServlet",因为后者是相对路径,建议使用后者

代码解析

代码实现过程:我的tomcat path路劲为/temp , docBase为E:\myDocument\MyWork\WorkSpace\myEclipse\test\WebRoot。

当用户输入http://localhost:8080/temp/helloworld时:在ip地址后面的第一个为temp。

当tomcat发现请求的上下文路径为temp,tomcat就会根据temp这个逻辑地址查找物理路劲,即为docBase的路劲。紧接发现后面查找的是helloworld资源,他会查找web.xml。比较其中的servlet-mapping,正好匹配<url-pattern>/helloworld</url-pattern>,发现名字为<servlet-name>helloworld</servlet-name>,再匹配servlet,发现与<servlet-name>helloworld</servlet-name>匹配,接着就找到<servlet-class>com.study.servlet.HelloWordServlet</servlet-class>生成class(这个过程在doget中传入的实例化对象是tomcat生成的),因为http请求的默认请求为get方法,所以com.study.servlet.HelloWordServlet就执行doget(),输出了helloworld。其实归根结底就是逻辑路劲映射物理路径,执行相应程序语言罢了。

对于jsp改变以后不用服务器重启(不需要在web,xml中定义),只有serclet改完以后或者设置改变以后需要需要重启一般来说,其实servlet是潜入了html代码的java,jsp是嵌入有java代码的html。

方法中的HttpServletRequest req,req其实是tomcat根据多态传入放入对象,其实质是一个门面模式。Javaee其实是一个规范,不是一种具体的实现,tomcat其实是一个实现,会把具体东西实现出来,不同的服务器有不同的实现形式,作为实际用户来说,我们只需要根据具体接口来实现就好了,我们不需要去考虑如何去实现,这是服务商该做的事

生成静态页面流程图

也就是说,浏览器不会直接和Servlet(包括get、post等)打交道,浏览器永远是和Tomcat(服务器)打交道的

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值