目录
目录
[1 JavaBean](#1 JavaBean)
[2 servlet](#2 servlet)
[1 新建servlet](#1 新建servlet)
[2 配置](#2 配置)
1 JavaBean
(1)概念
上节课我们讲了Jsp的基本开发流程和Jsp的内置对象,这种开发的实质是把java代码嵌入到html当中。这对复杂的项目是不可取的,而且也没有体现出面向对象的概念。
为了把业务逻辑封装起来,使用java代码封装为类,再用jsp调用,这种java类就是javabean组件。
下图为纯jsp开发模式

下图为java bean开发模式

说白了就是把字符串处理和数据库操作都交给javabean。
从eclipse上看,javabean的java内容被放到了src里,而jsp在web里。这很不一样。
写在src里做字符串和数据库的操作,感觉有点像是后端工作。jsp就变成了前端工作。

(2)分类
最开始java bean主要用于swing可视化开发,现在web开发其实都用不到了,所以web开发里都是用的非可视化java bean。这个概念了解一下就行。
(3)使用
java bean的使用主要依赖几个动作标签:<jsp:useBean><jsp:getProperty><jsp:setProperty>
下面通过个人信息管理的实例来分析
            
            
              html
              
              
            
          
          <%@ page language="java" contentType="text/html; charset=UTF-8"
    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=UTF-8">
<title>Insert title here</title>
</head>
<body>
	<%request.setCharacterEncoding("UTF-8");%>
	<jsp:useBean id="person" class="com.lyq.bean.Person" scope="page">
		<jsp:setProperty name="person" property="*"/>
	</jsp:useBean>
	<table align="center" width="400">
		<tr>
			<td align="right">姓 名:</td>
			<td>
				<jsp:getProperty property="name" name="person"/>
			</td>
		</tr>
		<tr>
			<td align="right">年 龄:</td>
			<td>
				<jsp:getProperty property="age" name="person"/>
			</td>
		</tr>
		<tr>
			<td align="right">性 别:</td>
			<td>
				<jsp:getProperty property="sex" name="person"/>
			</td>
		</tr>
		<tr>
			<td align="right">住 址:</td>
			<td>
				<jsp:getProperty property="add" name="person"/>
			</td>
		</tr>
	</table>
</body>
</html>
        首先用useBean来引入person类,然后对实例进行了*默认赋值。在这里实例获得了从上一页页面中form传来的参数。
之后在table控件中,通过getProperty来逐个填写。这样就显示在表中了。
2 servlet
servlet其实是基础。jsp就是解析为servlet运行的。其本质是按servlet规范写的java类。

(1)代码结构
通常说servlet就是指HttpServlet对象,在新建servelt时 其实就是新建1个java类,且继承于HttpServlet类。继承后可重写HttpServlet中的方法。
            
            
              java
              
              
            
          
          import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class TestServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	//初始化方法
	public void init() throws ServletException {
	}
	//处理HTTP Get请求
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
	}
	//处理HTTP Post请求
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
	}
	//处理HTTP Put请求
	public void doPut(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
	}
	//处理HTTP Delete请求
	public void doDelete(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
	}
	//销毁方法
	public void destroy() {
		super.destroy();
	}
}
        1 初始化方法
2 get请求
3 post请求
4 put请求
5 delete请求
6 销毁方法
注意:上面这段代码没法运行,只是个框架。
(2)常用接口
servlet运行需要serveley容器支持。servlet对象实现了java.servlet.Servet接口。该接口包含5个方法。

当客户端请求到来时,service方法会处理。
此外还有:
servletConfig
HttpServletRequest
HttpServletResponce
GenericServlet
HttpServlet
(3)如何开发
创建有2种方式,1是普通java类,继承httpServlet,再在web.xml中配置注册
另1种是在eclipse中新建servlet。
1 新建servlet

            
            
              java
              
              
            
          
          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;
@WebServlet("/stest1")
public class stest1 extends HttpServlet {
	
	// 序列化
	private static final long serialVersionUID = 1L;
       
	// 构造方法
    public stest1() {
        super();
    }
    // get
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.getWriter().append("Served at: ").append(request.getContextPath());
	}
	// post
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}
}
        2 配置
需要在web.xml里配置三项:
            
            
              XML
              
              
            
          
          <?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0">
  <display-name>test2</display-name>
  <servlet>
    <servlet-name>MyServlet</servlet-name>
    <servlet-class>test2.stest1</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>MyServlet</servlet-name>
    <url-pattern>/servlet/stest1</url-pattern>
  </servlet-mapping>
  <welcome-file-list>
    <welcome-file>servlet/stest1</welcome-file>
  </welcome-file-list>
</web-app>
        配置好后,运行tomcat,在网址里查看

可见,servlet也是网页打开,但页面没有后缀。
那么考虑一个问题,电商项目无法连接数据库,是不是也是这个配置问题?