版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
一、 创建工程
1.在myeclipse中创建一个web工程,名称为ch1
2.在ch1工程中的WebRoot文件夹下,创建一个JSP文件,名称为eg1.jsp
3、编辑eg1.jsp文件
<%@ 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>My JSP '3-eg1.jsp' starting page</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>
<%!int number=1; //number为全局变量,每刷新一次对应的number加1 %>
<%--这是申明一个变量 --%>
<%!public int count()
{
return number++;
//<%!的方式来声明一个方法 int类型必须有返回值
} /*这是申明一个方法*/
%>
<%
//JSP程序代码
out.println("Hello JSP");//out.println("1123");用来打印字符串
out.println("欢迎使用JSP交互动态网页");
//<%=的方式来写输出函数 "A"+count()+"B" 字符串A+函数+字符串B
%>
<br>
<%="您是第"+count()+"个客人!"%>
<br>
</body>
</html>
二、配置服务器并导入文件
1.导入Tomcat 7.X服务器到myeclipse中
2.将Tomcat 7.X服务器设为本地服务器
3、将ch1工程导入到Tomcat 7.X服务器中
四、在本地Tomcat服务器上运行工程
输入地址:http:\localhost:8080\ch1\eg1.jsp
即可运行对应的文件