目录
使用web.xml设置网页首选项
首先在创建项目时,点击next出现下方页面,需要勾选Generate web.xml deployment descriptor 这个选项,创建web.xml文件
然后在此项目的目录下会生成一个web.xml的文件
web.xml文件所在路径:项目文件夹->src->main->webapp->WEB-INF->web.xml
双击点开并打开Source界面
在此界面下中 welcom-file-list 标签下查询界面顺序为从上至下,可以删除多余标签留下一个需要为首选项的标签,内容为需要访问的文件名即可
注意:修改了 web.xml 文件,其项目必须重新在服务器上重新启动
jsp的标准标签
jsp的常用标签
- <jsp:include page="页面"> 作用:使这个页面包含其他页面
- <jsp:param name="name" value="value"> 作用:将参数传递给其他页面
- <jsp:foward page="页面"> 作用:转发至其他页面
- <jsp:useBean> 作用:实例化一个对象
- <jsp:setProperty> 作用:给useBean属性设置值
- <jsp:getProperty> 作用:在useBean中取值
useBean的用法
<jsp:useBean id="id" beanName="bn" type="type" class="class" scope="scope">
id:对象名 *
class:类 创建对象时需要完全限定名(包名+类名)
type:类型 调用对象时 * (可以用抽象父类或者接口)
scope:作用域(page * request session application)
setProperty的用法
<jsp:setProperty name="name" property="property" value="value">
name:useBean 的id
property:属性名(要注意必须跟实体类中的属性名保持一致)
value:属性值
jsp标准标签的使用
用户类 User.java
package com.pojo;
public class User {
private int userid;
private String username;
private String userpwd;
public int getUserid() {
return userid;
}
public void setUserid(int userid) {
this.userid = userid;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getUserpwd() {
return userpwd;
}
public void setUserpwd(String userpwd) {
this.userpwd = userpwd;
}
@Override
public String toString() {
return "User [userid=" + userid + ", username=" + username + ", userpwd=" + userpwd + "]";
}
public User(int userid, String username, String userpwd) {
super();
this.userid = userid;
this.username = username;
this.userpwd = userpwd;
}
public User() {
// TODO Auto-generated constructor stub
}
}
登录界面 login.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>login</title>
</head>
<body>
<h1>登录界面</h1>
<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>
<!-- u即是定义了一个名为u的变量,class是这个变量的类型 -->
<jsp:useBean id='u' class='com.pojo.User'></jsp:useBean>
<!-- setProperty意为设置属性,这里相当于给对象u设置了username属性,值是sa,等价于java代码中的u.setUsername("sa"); -->
<jsp:setProperty property="username" name="u" value="sa"/>
<!-- getProperty意为得到属性,得到对象u中的username属性 -->
<jsp:getProperty property="username" name="u"/>
<form action="index.jsp">
<div><input type="text" name="username"></div>
<div><input type="text" name="password"></div>
<button type="submit">登录</button>
</form>
<%out.println(u.toString()); %>
</body>
</html>
主页 index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>index</title>
<style type="text/css">
.h{
border: 10px solid red;
height: 150px;
weith:100px;
}
</style>
</head>
<body>
<% String title="";
String type=request.getParameter("type");
if("1".equals(type)){
title="一号标签";
}if("2".equals(type)){
title="二号标签";
}if("3".equals(type)){
title="三号标签";
}
%>
<h1 class="h"><%=title %></h1>
</body>
</html>
你要尽全力保护你的梦想。那些嘲笑你梦想的人,他们注定失败,他们想把你变成和他们一样。我坚信,只要心中有梦想,我就会与众不同。你也是。