常用标签
1.<jsp:include page=“页面”> 包含
2.<jsp:param name=“name” value=“va”> 传参
3.<jsp:foward page=“页面”> 转发
4.jsp:useBean相当于实例化类
5.jsp:setProperty给useBean属性设置值
6.jsp:getProperty取值
useBean的用法:
<jsp:useBean id="" beanName="" type="" class="" scope="">
id: 对象名 *
class:类 创建对象时,完全限定名(包名+类名)
type:类型 调用对象时 * (可以用抽象父类或者接口)
scope:作用域 (page * request session application)
setProperty的用法:
<jsp:setProperty name="" property="" value="">
name:useBean 的id
property:属性名(要注意必须跟实体类中的属性名保持一致)
value:属性值
下面来看一个小案例:
User.java:用户类
package com.zking.pojo;
public class User {
private Integer userid;
private String username;
private String password;
public Integer getUserid() {
return userid;
}
public void setUserid(Integer userid) {
this.userid = userid;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public User(Integer userid, String username, String password) {
super();
this.userid = userid;
this.username = username;
this.password = password;
}
public User() {
// TODO Auto-generated constructor stub
}
@Override
public String toString() {
return "User [userid=" + userid + ", username=" + username + ", password=" + password + "]";
}
}
login.jsp:登录页面
<%@ 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>
<h1>欢迎登陆</h1>
<!--
// type:
// 1.热门商品
// 2.折扣商品
// 3.人气商品
-->
<jsp:include page="index.jsp">
<jsp:param value="1" name="type"/>
</jsp:include>
<jsp:include page="index.jsp">
<jsp:param value="2" name="type"/>
</jsp:include>
<jsp:include page="index.jsp">
<jsp:param value="3" name="type"/>
</jsp:include>
<%--
<jsp:forward page="index.jsp"/ >
--%>
<%--<!-- --> 是html注释 会执行 这个是java注释--%>
<form action="doLogin.jsp">
<input name="userid"><br>
<input name="username"><br>
<input name="password"><br>
<button>登录</button>
</form>
</body>
</html>
doLogin.jsp:登录界面操作类
<%@page import="com.zking.pojo.User"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
User user=new User();
user.setUserid(1);
user.getUserid();
%>
<!-- User u=new User(); -->
<jsp:useBean id="u" class="com.zking.pojo.User"></jsp:useBean>
<!-- property 属性 -->
<!-- u.setUsername("haha"); -->
<jsp:setProperty property="userid" name="u" param="userid"/>
<jsp:setProperty property="username" name="u" param="username"/>
<jsp:setProperty property="password" name="u" param="password" />
<!-- u.getUsername("haha"); -->
<jsp:getProperty property="username" name="u" />
<%
u.getUserid();
out.print(u);
%>
index.jsp:主页
<%@ 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>
<style>
.h1{
border:10px solid pink;
height:200px;
}
</style>
</head>
<body>
<%
String type=request.getParameter("type");
//根据别人携带的参数做判断
// type:
// 1.热门商品
// 2.折扣商品
// 3.人气商品
String data="";
if("1".equals(type)){
data="热门商品";
}
if("2".equals(type)){
data="折扣商品";
}
if("3".equals(type)){
data="人气商品";
}
%>
<h1 class="h1"><%=data %></h1>
</body>
</html>
对了,在写这个项目之前记得先生成一个web.xml的文件,在创建项目的时候吧这个钩上即可
如图它会自动生成在lib项目下面:
好了今天的分享就到这了,下期见