JSP的基本标签

目录

一、使用web.xml设置网页默认跳转界面

 二、常用jsp标签

include和param的使用

 userBean用法

 setProperty用法

userBean和setProperty代码使用展示


一、使用web.xml设置网页默认跳转界面

1.新建项目时,先不要直接点击Finish,先点击Next

2.当点击到以下界面,把我圈起来那个勾起来,再点击Finish

如果没有勾选,在项目中是找不到web.xml的

3.可以在WEB-INF目录底下看到web.xml,双击web.xml,点击Source

4.进入以下界面 

5.修改代码如图所示  

 二、常用jsp标签

<jsp:include page="页面">包含
<jsp:param name="name" value="value">传参
<jsp:foward page="页面">转发
<jsp:useBean>相当于实例化类 
<jsp:setProperty>给useBean属性设置值
<jsp:getProperty>取值

include和param的使用

💕显示界面

<%@ 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>

<!-- 转发 -->
<%-- 必须使用java注释
<jsp:forward page="index.jsp"/>
 --%>

</body>
</html>

<!--  -->  HTML注释

<%--  --%>  java注释

jsp使用HTML注释会不生效,jsp需使用Java注释

💕处理显示的界面

<%@ 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 yellow;
		height:100px;
	}
</style>

</head>
<body>
<!-- 
	type:
		1.热门商品
		2.折扣商品
		3.人气商品
 -->
<%
	String type=request.getParameter("type");
	String data="";
	if("1".equals(type)){
		data="热门商品";
	}
	if("2".equals(type)){
		data="折扣商品";
	}
	if("3".equals(type)){
		data="人气商品";
	}
%>
<h1 class="h1"><%=data %></h1>

</body>
</html>

💖效果展示 

 userBean用法

<jsp:useBean id="" beanName=""  type=""  class="" scope="">

id: 对象名 * 

class: 类  创建对象时,完全限定名(包名+类名)

type: 类型 调用对象时 *  (可以用抽象父类或者接口)

scope: 作用域 (page *  request session  application)

 setProperty用法

<jsp:setProperty  name=""  property=""  value="" param="">

name:useBean 的id

property:属性名(要注意必须跟实体类中的属性名保持一致)

value:属性值

param:拿到用户传入的值,相当于 request.getParameter("")

userBean和setProperty代码使用展示

💕用户实体类 User.java

package com.xyz.pojo;

public class User {

	private Integer id;
	private String username;
	private String password;
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	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() {
		// TODO Auto-generated constructor stub
	}
	public User(Integer id, String username, String password) {
		super();
		this.id = id;
		this.username = username;
		this.password = password;
	}
	@Override
	public String toString() {
		return "User [id=" + id + ", 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>
 
 <form action="doLogin.jsp">
 	<p><input name="id"></p>
 	<p><input name="username"></p>
 	<p><input name="password"></p>
 	<p><button>登录</button></p>
 </form>

</body>
</html>

💕处理登录的界面 doLogin.jsp

<%@page import="com.xyz.pojo.User"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%
	User user=new User();
	user.setId(1);
	user.getId();
%>
<!-- User u=new User(); -->
<jsp:useBean id="u" class="com.xyz.pojo.User"></jsp:useBean>
<!-- param 相当于 request.getParameter("id") -->
<jsp:setProperty property="id" name="u" param="id"/>
<jsp:setProperty property="username" name="u" param="username"/>
<jsp:setProperty property="password" name="u" param="password"/>

<%-- 
<!--设置u的属性 u.setUsername("haha") -->
<jsp:setProperty property="username" name="u" value="haha"/>

<!--获得u的属性 u.getUsername() -->
<jsp:getProperty property="username" name="u"/>
--%>

<%
	out.print(u);
%>

💖效果展示  

 今天就分享到这啦,下期给大家分享我们经常用到的购物车

  • 12
    点赞
  • 34
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值