一、使用web.xml文件设置网页首选项
第一步:首先在新建项目时,先莫着急点击finish,咱应该先点next
第二步:再点next
第三步: 选择勾选 Generate web.xml deployment descriptor,最后就再点击finish
注意:在设置完网页首选项之后,一定要重启服务
二、jsp的基本标签
1、 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 >取值
2、useBean的用法
<jsp:useBean id=“” beanName=“” type=“” class=“” scope=“”>
id: 对象名 *
class:类 创建对象时,完全限定名(包名+类名)
type:类型 调用对象时 * (可以用抽象父类或者接口)
scope:作用域 (page * request session application)
3、 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>