目录
新建带xml的Web项目:
首先右键新建项目
然后
建好的xml在WEB-INF文件里
常用jsp标签(自带):
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>取值
userBean用法:
<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:属性值
接下来我们运用一下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>
<!-- html注释 -->
<%--java注释 --%>
<%--必须用java注释
<jsp:forward page="index.jsp"/>
--%>
<form action="dologin.jsp">
<input name="id"><br>
<input name="username"><br>
<input name="password"><br>
<button>登录</button>
</form>
</body>
</html>
主页面代码
<%@ 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 red;
height: 200px;
}
</style>
</head>
<body>
<%
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>
写一个pojo包,建一个User类,记得导jar包
package com.zking.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 + "]";
}
}
处理登陆代码
<%@page import="com.zking.pojo.User"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
User user=new User();
user.setId(1);
user.getId();
%>
<jsp:useBean id="u" class="com.zking.pojo.User"></jsp:useBean>
<jsp:setProperty property="id" name="u" param="id" />
<jsp:setProperty property="username" name="u" param="username" />
<jsp:setProperty property="password" name="u" param="password"/>
<jsp:getProperty property="username" name="u" />
<%
u.getId();
out.print(u);
%>
xml代码
记住修改了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>Web13</display-name>
<!--web.xml如果被修改,服务器必须重启 -->
<welcome-file-list>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list>
</web-app>
今天就到这了,拜拜ヽ(✿゚▽゚)ノ。