现在的位置: 首页 > 综合 > 正文

3、servlet程序剖析及web.xml详解

2018年02月06日 ⁄ 综合 ⁄ 共 6572字 ⁄ 字号 评论关闭

WebRoot目录下有WEB-INF/classes子目录,用于存放编译的class文件,web.xml叫做部署描述符,是web应用的核心文件,WebRoot\WEB-INF\lib存放WEB应用使用的第三方库

对于tomcat中配置的<Context path="/test" docBase="d:/test/WebRoot" reloadable="true" />,path是一个上下文路径,是个逻辑路径,docBase 是逻辑路径对应的物理路径,这样就屏蔽了物理路径,确保系统安全。reloadable是对于class文件进行修改后,自动重新加载class到内存中。否则,就需要不停地重新启动服务器。

1、MyEclipse集成服务器:window——>preference——>myeclipse——>servers——>tomcat

2、项目部署,Myeclipse中,使用deploy myeclipse J2EE project to server,将项目部署到tomct上,实际进行的操作是将myeclipse中的项目的webroot目录拷贝到tomcat的webapps目录下,并修改目录名为项目名称。部署还可以以.war形式部署,将.war放到tomcat的webapps下,tomcat运行时会自动将此war文件解压,形成前面说的目录结构,这时war文件作用就完成了,以后的访问都是访问解压后的目录及文件。

3、Servlet是JAVA服务器端编程,不同于我们之前写的一般的JAVA应用程序,Servlet程序是运行在服务器上的,服务器有很多种,我们使用的是tomcat。

4、第一个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 HelloServlet extends HttpServlet
{
	@Override
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException
	{
		resp.setContentType("text/html");
		PrintWriter out = resp.getWriter();
		out.println("<html><head><title>hello world</title></head>");
		out.println("<body><h1>hello,servlet ,</h1></body></html>");
		out.flush();
	}
}

编写完servlet后,怎样访问呢?需要配置web.xml,第一个servlet的配置如下:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
	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">
  
  <servlet>
  	<servlet-name>Hello</servlet-name>
  	<servlet-class>com.cdtax.servlet.HelloServlet</servlet-class>
   </servlet>
   
   <servlet-mapping>
		<servlet-name>Hello</servlet-name>
		<url-pattern>/Hello</url-pattern>
   </servlet-mapping>
</web-app>

web.xml中配置servlet的几个要点:

1)<servlet></servlet>与<servlet-mapping></servlet-mapping>配合完成servlet的配置
2)<servlet>中的<servlet-name>内容要与<servlet-mapping>中的<servlet-name>内容一致(大小写是敏感的)
3)<servlet-class>类的名称要写正确,写类的全名(包括包名)
4)<url-pattern>中的内容第一位要有一个反斜线/
5)<servlet-name>名字是随意起的,只要符合2)的规定,但是一般以类的名字去掉Servlet来命名,这里就是Hello(类HelloServlet的头几个单词),(可以做实验,将其改为其他任意字符,效果一样)
6)<url-pattern>内容也是可以随意写的,是我们访问的标识,就是我们在浏览器地址栏http://ip:port/test后面所要写的内容,按照上面的配置,在本地配置的tomcat上部署,输入http://localhost:8080/test/Hello,(实验,将/Hello修改为其他任意内容,如/aabbc,访问http://localhost:8080/test/aabbc,效果一样)
7)建议servlet-name和url-pattern写有意义的字符,因为servlet可能很多,将来查找辨认方便。

5、访问一个servlet的整个过程:当我们在地址栏输入http://localhost:8080/test/Hello,请求传递到tomcat服务器,服务器先查找/test,就是我们在server.xml中配置的Context的path,如果匹配,则转到web.xml查找是否有/Hello的url-pattern,如果有,如本例,找到其servlet-name(这里是Hello),然后再找到其class文件(这里是com.cdtax.servlet.HelloServlet)加载并自动运行doGet方法。

6、第一个jsp:

<%@ 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 'MyJsp.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.这是我的第一个 jsp<br>
    <h1>time: <%= new java.util.Date().toLocaleString() %></h1>
  </body>
</html>

我们可以将servlet看作是嵌套了HTML代码的java类可以将jsp看做是嵌套了java代码的HTML。

7、一个jsp登录页面调用一个servlet打印出登录用户名、密码:

<%@ 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 'login.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>
    <form action="/webp1/LoginServlet">
    
    	username:<input type="text" name="username"><br>
    	password:<input type="password" name="password"><br>
    	<input type="submit" value=" submit" >  
    	<input type="reset" value="reset" >
     </form>
  </body>
</html>

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 LoginServlet extends HttpServlet
{
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException
	{
		String username = req.getParameter("username");
		String password = req.getParameter("password");
		
		resp.setContentType("text/html");
		PrintWriter out = resp.getWriter();
		
		out.println("<html><head><title>Login result</title></head>");
		
		out.println("<body>username:" + username + "<br>");
		out.println("pasword:" + password + "</body></html>");
		
	}
}

web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
	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">
  
  <servlet>
  	<servlet-name>Hello</servlet-name>
  	<servlet-class>com.cdtax.servlet.HelloServlet</servlet-class>
   </servlet>
   
  <servlet>
  	<servlet-name>LoginServlet</servlet-name>
  	<servlet-class>com.cdtax.servlet.LoginServlet</servlet-class>
  </servlet>
  
   <servlet-mapping>
		<servlet-name>Hello</servlet-name>
		<url-pattern>/Hello</url-pattern>
   </servlet-mapping>
   
   <servlet-mapping>
   	<servlet-name>LoginServlet</servlet-name>
   	<url-pattern>/LoginServlet</url-pattern>
   </servlet-mapping>
</web-app>

servlet的doGet方法中的参数req和resp,是由Tomcat生成的对象,具体为org.apache.catalina.connector.RequestFacade@xxx和org.apache.catalina.connector.ResponseFacade@xxx,是容器生成的对象。

jsp的form标签中的action属性指出了表单提交的对象,即这个form提交给谁,这里给出了我们所写的LoginServlet,tomcat将请求来的信息包装到req对象中,这些信息就包含了username=xxx和password=xxx。而req对象的getParameter()方法能够根据给定的参数(变量名,如username)返回相应的值(变量值)

form标签中的method属性指出提交的方法,有两种方法get和post,他们的区别主要在于HTTP请求的不同:

1)浏览器地址栏呈现的结果不同(表象)
2)真正原因在于向服务器端发送请求时的形式不同:
get的请求格式:  GET /webp1/LoginServlet?username=hhhhh&password=ppp HTTP/1.1  (CRLF)
post的请求格式:
POST /webp1/LoginServlet HTTP/1.1 (CRLF)

...

Connection:Keep-Alive

username=hhh&pasword=ppp

通过浏览器进行文件上传时,一定要使用post方式而绝不能使用get方式。

通过浏览器地址栏输入网址的方式来访问服务器端资源,全部使用的是get方法请求的

8、使用Servlet生成静态页面的流程:


浏览器永远不会直接同servlet打交道,只能同服务器打交道,由服务器来访问servlet,servlet处理结果由服务器返回给浏览器。

抱歉!评论已关闭.