您的位置:首页 > Web前端 > JavaScript

jsp入门学习

2016-03-31 16:09 513 查看
<%@ page language="java" contentType="text/html; charset=UTF-8"

pageEncoding="UTF-8"%><!-- ISO-8859-1 -->

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<%@ page import="java.util.Date" %>

<%@ page import="java.text.SimpleDateFormat" %>

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<!-- eclipse File >> new >> Dynamic Web Project -->

<!-- 第一个jsp应用

<title>这是一个JSP简单界面——显示系统时间</title>

</head>

<body>

<%

Date date = new Date();

SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

String today = format.format(date);

%>

当前时间<%=date %>

</body> -->

<!-- -->

<!-- 第二个jsp应用 被包含的文件不能有重名的变量和方法

<title>使用文件include 指令</title>

</head>

<body style="margin:0px;">

<%@ include file="top.jsp" %>

<table width="781" height="271" border="0" cellpadding="1" cellspacing="1" >

<tr>

<td> </td>

</tr>

</table>

<%@ include file="copyright.jsp" %>

</body> -->

<!-- -->

<!-- 第三个jsp应用

<title>输出乘法口诀</title>

</head>

<body>

<%

String str = "";

for(int i=1; i<=9; i++){

for(int j=1; j<=i; j++){

str += i + "*" +j +"=" + i*j;

str += " ";

}

str += "<br>";

}

%>

<table width="440" height="85" border="1" cellpadding="0" cellspacing="0" style="font: 9pt"

bordercolor="FFFFFF">

<tr>

<td height="30" align="center">九九乘法表 </td>

</tr>

<tr>

<td style="padding: 5pt"><%=str %> </td>

</tr>

</table>

<%!

/**

可以在html中显示的注释

count 访问次数

*/

//可以在html中显示的注释 count 访问次数

int count = 0;

int count(){

return count++;

}

%>

/**

*可以在html中显示的注释

*count 访问次数

*/

//可以在html中显示的注释 count 访问次数

<%-- 隐藏的注释 --%>

<p>访问次数为<%=count() %></p>

</body> -->

<!--第四个jsp应用

<title>用page include 包含的应用</title>

</head>

<jsp:include page="top.jsp"></jsp:include>

<body style="margin: 1px">

<table width="741" height="271" border="1" cellpadding="0" cellspacing="0" background="#777777">

<tr> </tr>

</table>

<jsp:include page="copyright.jsp"></jsp:include>

</body> -->

<!--

被包含的文件不能有重名的变量和方法

jsp:include page="url" flush="false|true">

子动作标识 jsp:param>

/jsp:include

page: 用于指定被包含文件的相对路径。例如,指定属性值为top.jsp,则表示包含的是与当前JSP文件相同

文件夹中的top.jsp文件包含到当前JSP页面中。

flush:可选属性,用于设置是否刷新缓冲区,默认值为flase,如果设置为true,在当前页面输出使用了缓冲

区的情况下,先刷新缓冲区,然后再执行包含工作。

子动作标识 jsp:param>:用于向被包含的动态页面中传递参数。关于 jsp:param>
-->

<!-- -->

<title>forward的使用</title>

</head>

<body>

request.setAttribute("nam", "user");

request.setAttribute("pwd", "123456");

<jsp:forward page="login.jsp">

<jsp:param value="user1" name="user1" />

<jsp:param value="passwd" name="1234"/>

</jsp:forward>

<!-- <a href="login.js" user1="user1" passwd="1234"></a> -->

<!-- <a href="login.js?user1=user1&passwd=1234"></a> -->

</body>

</html>

============================

top.jsp

<%@ page

pageEncoding="UTF-8"%>

<img src="image/renche.jpg">

===========================

copyright.jsp

<%@ page pageEncoding="UTF-8"%>

<%

String copyright="  All CopyRight © 2016 一公司";

%>

<table width="778" height="61" border="1" cellpadding="0" cellspacing="1"

background="image/bg.jpg">

<tr>

<td><%=copyright %></td>

</tr>

</table>

==============================

login.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"

pageEncoding="UTF-8"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>登录界面</title>

</head>

<body>

<%

String user = request.getParameter("user1");

String pwd = request.getParameter("passwd");

String name = request.getAttribute("nam").toString();

String passwd = request.getAttribute("pwd").toString();

%>

<form name="form1" method="post" action="">

用户名<input type="text" name="name" id="name" style="width: 200px" value=<%=user %>><br>

密  码<input type="password" name="pwd" id="pwd" style="width: 200px" value=<%=pwd%>>

<br><br>

<input type="submit" name="submit" value="提交">

</form>

request设置的user的值:<%=name %><br>

request设置的passwd的值:<%=passwd %><br>

</body>

</html>

那两张图片就不贴了,在WebContent目录下新建一个image文件,这个文件放那两张图片
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: