JavaBean
实体类,一般用来和数据库的字段做映射ORM
特定的写法
- 必须要有一个无参构造
- 属性必须私有化
- 必须有对应的get/set方法
ORM:对象关系映射
- 表->类
- 字段->属性
- 行记录->类的对象
<%--javabean写法--%>
<%@page import="com.XZY_SUNSHINE.pojo.people" %>
<%@ page import="java.net.URLEncoder" %>
<%@page contentType="text/html;charset=utf-8" %>
<jsp:useBean id="someone" class="com.XZY_SUNSHINE.pojo.people" scope="page"/>
<jsp:setProperty name="someone" property="id" value="1"/>
<jsp:setProperty name="someone" property="name" value="hhh"/>
<jsp:setProperty name="someone" property="age" value="18"/>
<jsp:setProperty name="someone" property="address" value="china"/>
<jsp:getProperty name="someone" property="id"/><br>
<jsp:getProperty name="someone" property="name"/><br>
<jsp:getProperty name="someone" property="age"/><br>
<jsp:getProperty name="someone" property="address"/>
<%--java写法--%>
<hr>
<%
people people = new people();
people.setName("www");
people.setAge(19);
people.setAddress("中国");
people.setId(2);
out.println(people.getId()+"<br>");
out.println(people.getName()+"<br>");
out.println(people.getAge()+"<br>");
out.println(people.getAddress()+"<br>");
%>
MVC三层架构
介绍
MVC:model view controller 模型、试图、控制器
以前
现在
Model
- 业务处理:业务逻辑(service)
- 数据持久层:CRUD(DAO)
View
- 展示数据
- 提供链接发起servlet请求
Controller(Servlet)
- 接受用户的请求
- 交给业务层处理对应的代码
- 控制视图的跳转
过滤器、监听器
过滤器:filter
介绍
用来过滤网站的数据,web服务器启动就已经初始化了,服务器关闭的时候,过滤器才会销毁。
- 处理中文乱码
- 登陆验证
步骤
-
导包
-
编写过滤器
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
servletRequest.setCharacterEncoding("utf-8");
servletResponse.setCharacterEncoding("utf-8");
servletResponse.setContentType("text/html;charset=utf-8");
//让我们的请求继续往下走,如果不写的话,将会停在这里
filterChain.doFilter(servletRequest,servletResponse);
}
<filter>
<filter-name>encodingfilter</filter-name>
<filter-class>com.XZY_SUNSHINE.fiflter.encodingfilter</filter-class>
</filter>
<filter-mapping>
<filter-name>encodingfilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
监听器
public class session implements HttpSessionListener {
public void sessionCreated(HttpSessionEvent se) {
ServletContext servletContext = se.getSession().getServletContext();
Integer count = (Integer) servletContext.getAttribute("count");
int onlinecount=0;
if (count==null){
onlinecount=1;
}else{
onlinecount=count.intValue()+1;
}
servletContext.setAttribute("count",new Integer(onlinecount));
}
public void sessionDestroyed(HttpSessionEvent se) {
}
}
<listener>
<listener-class>com.XZY_SUNSHINE.listener.session</listener-class>
</listener>
filter实现权限拦截
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) servletRequest;
HttpServletResponse response = (HttpServletResponse) servletResponse;
User user = (User) request.getSession().getAttribute(constants.USER_SESSION);
if (user==null){
response.sendRedirect(request.getContextPath()+"/error.jsp");
}else{
filterChain.doFilter(servletRequest,servletResponse);
}
}
<filter>
<filter-name>LoginFilter</filter-name>
<filter-class>com.XZY_SUNSHINE.filter.LoginFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>LoginFilter</filter-name>
<url-pattern>/jsp/*</url-pattern>
</filter-mapping>
``