项目实现在网页中显示学生的学号、姓名、性别、年龄、年级等。
1.首先新建一个web工程(Web project),然后在src中建一个包叫entity。
2.在entity包中建一个java文件Student。
package entity;
public class Student {
public int id;
public String name;
public String sex;
public String grade;
public int age;
public Student() {
this.id = 6;
this.name =" 张国盛";
this.sex ="男";
this.grade = "12级网络1班";
this.age = 22;
}
}
3.在WebRoot中的index.jsp中写所需要显示的主页代码
<%@page import="entity.Student" %>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>显示学生信息</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<%
Student stu=new Student();
out.print("学号:"+stu.id);
out.print("姓名:"+stu.name);
out.print("性别:"+stu.sex);
out.print("班级:"+stu.grade);
out.print("年龄:"+stu.age);
%>
</body>
</html>
4.在Servers中开启Tomacat服务器,然后启动项目。
显示为
也可以访问网站http://localhost:8080/test/index.jsp