javaBean
将jsp中登陆操作的代码 转移到了loginDao。java;其中loginDao类就称之为javaBean
javaBean的作用:a 减轻jsp的复杂度 b 提高代码复用(以后任何地方的登陆操作都可以调用loginDao实例
javaBean(就是一个javaBean)的定义:
a public 修饰的类,public无参构造
b 所有属性都是private,并且提供set/get (如果boolean 则 get可以替换成 is)
使用层面, javabean分为两大类
a 封装业务逻辑的javaBean --------------------------------------------- 逻辑
b 封装数据的javaBean(实体类(对应数据库的一张表) ------- 数据
MVC设计模式
M:model 模型L: 一个功能 用javabean实现
V:View 视图: 用于展示。以及于用户交互。使用html,js,css,jsp,jquery等前端技术实现
C:Connroller,控制器,接受请求,将请求跳转到模型进行处理;模型处理完后,再将处理的结果返回给 请求处。可以使用jsp实现, 但是一般建议使用Servlet实现控制
Servlet
java类必须符合一定的规范:
a 必须继承 javax.servlet.http.HttpServlet
b 重写其中的doGet(),doPost()方法
doGet():接受并处理所有get提交方式的请求
doPost();接受并处理所有post提交方式的请求
Servlet要想使用,必须配置
servlet2.5: web·xml
Servlet3.0: @Servlet
servlet2.5: web·xml (要导入servlet.api.jar)
右键项目->builtPath -> cofig…->resource->class文件放入WEB-INF的classes下
项目的根目录:webContent。src
ml 所在的jsp实在webContent目录下中,因此发出的请求welcomeServlet是去请求项目的根目录
code
welcomeServlet.java
package servlet;
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 welcomeServlet extends HttpServlet{
private static final long serialVersionUID = -3905726377040512044L;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("doGet...");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doGet(req,resp);
}
}
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>webServlet1</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>welcome</servlet-name>
<servlet-class>servlet.welcomeServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>welcome</servlet-name>
<url-pattern>/welcome</url-pattern>
</servlet-mapping>
</web-app>
index.jsp
<%@ 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>
<a href = "welcome">welcome</a>
</body>
</html>