1.userBean用法
相当于实例化类
<jsp:useBean id="" beanName="" type="" class="" scope="">
- id指的是对象名字
- class指的是类名,创建用户时,完全限定名(包名+类名)
- type指的是类型,调用对象时,可以用抽象分类或者接口
- scope指的是作用域 (page * request session application)
2.setProperty与getProperty的用法
分别是用来给userBean设置值和取值。
setProperty:
<jsp:setProperty name="" property="" value="">
getProperty:
<jsp:getProperty property="" name=""/>
- name:useBean 的id
- property:属性名(要注意必须跟实体类中的属性名保持一致)
- value:属性值
3.include与param的配套使用
index界面:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<h1>这是主页</h1>
<jsp:include page="login.jsp">
<jsp:param value="phone" name="good"/>
</jsp:include>
<jsp:include page="login.jsp">
<jsp:param value="car" name="good"/>
</jsp:include>
<jsp:include page="login.jsp">
<jsp:param value="home" name="good"/>
</jsp:include>
login界面:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
String str=request.getParameter("good");
if("phone".equals(str)){
str="手机急么便宜,还不快入手?";
}
if("car".equals(str)){
str="你还在挤公交吗,你还在因为上班不方便而烦恼,快入店来看看吧!";
}
if("home".equals(str)){
str="房价大跌,现在不出手,啥时候出手?";
}
%>
<h1 id="h1"><%=str%></h1>
</body>
</html>
页面显示:
4.jsp转发
与Java中的转发产生的效果是一致的。
<jsp:forward page=""></jsp:forward>
page指的是跳转的界面。
5.web.xml的使用
在我们创建项目的时候,在下面这个界面勾选以下内容,生成项目就会自动创建web.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"
id="WebApp_ID" version="4.0">
<display-name>web05</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>
</web-app>
以上就是该文件中的内容,<welcome-file>界面路径</welcome-file>就是指定初始的跳转界面,如果第一个界面没有就会往下面走,直到找到有效的界面。默认就是index.hmtl的跳转路径。
设置初始位置为登录。
<?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" id="WebApp_ID" version="4.0">
<display-name>web13</display-name>
<welcome-file-list>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list>
</web-app>
注意:修改了web.xml就必须重启服务器。