访问默认为某个界面
先创建一个文件夹
图片中圈起来的那个需要在创建文件的时候打钩,这个是干什么的呢?创建web.xml文件并发布
作用:访问默认为某个界面
怎么进行设置呢?
首先找到web.xml文件
点击进入后再点击源码(Source)
<?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>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>
就可以进行更改啦
注意:web.xml如果被修改,服务器必须重启。
常用jsp标签(自带):
1.<jsp:include page="页面"> 包含
2.<jsp:param name="name" value="va"> 传参
同样的设计但有不同的分类,就需要包含和传参两个功能
首页
<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>
包含的界面
<%@ 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");
//根据别人携带的参数做判断
//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>
效果图:
3.<jsp:foward page="页面"> 转发
<jsp:forward page="index.jsp"></jsp:forward>
打开页面直接转发到转发中添加的界面中
<!--
<jsp:forward page="index.jsp"></jsp:forward>
-->
这样写这个代码会运行吗?
答案是会,为什么呢?因为在jsp代码里面java先运行的
4.<jsp:useBean>相当于实例化类
userBean用法:
<jsp:useBean id="" beanName="" type="" class="" scope="">
id: 对象名 *
class:类 创建对象时,完全限定名(包名+类名)
type:类型 调用对象时 * (可以用抽象父类或者接口)
scope:作用域 (page * request session application)
5.<jsp:setProperty>给useBean属性设置值
setProperty用法:
<jsp:setProperty name="" property="" value="">
name:useBean 的id
property:属性名(要注意必须跟实体类中的属性名保持一致)
value:属性值
6.<jsp:getProperty>取值
4、5、6标签的实例:
登录界面
<%@ 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">
<input name="userId"><br>
<input name="userName"><br>
<input name="password"><br>
<button>登录</button>
</form>
</body>
</html>
创建pojo包编写User类
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() {
super();
// TODO Auto-generated constructor stub
}
public User(Integer userId, String userName, String password) {
super();
this.userId = userId;
this.userName = userName;
this.password = password;
}
@Override
public String toString() {
return "User [userId=" + userId + ", 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.setUserId(1);
user.getUserId();
%>
<!-- User u=new User(); -->
<jsp:useBean id="u" class="com.zking.pojo.User"></jsp:useBean>;
<!-- 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(); -->
<jsp:getProperty property="userId" name="u"/>
<jsp:getProperty property="userName" name="u"/>
<jsp:getProperty property="password" name="u"/>
<%
u.getUserId();
out.print(u);
%>
今天就到这里啦,下次见咯,拜拜