Java Tutorial/JSP/UseBean
Версия от 17:44, 31 мая 2010; (обсуждение)
Содержание
- 1 Call Bean Constructor To Pass In Message
- 2 Call Setter In Java Bean To Change its Property Value
- 3 Fields of the Calendar Class
- 4 Get/Set Value Using JSP Set Property
- 5 Import JavaBeans In JSP Page
- 6 JSP Helper Bean HTMLFilter
- 7 JSP Number Guess Bean
- 8 Reference Calendar Bean
- 9 Reference Package Name in JSP Page
- 10 Using initialized field in jsp page
- 11 Using the Date Class
- 12 Using "useBean" To Reference A Java Bean
Call Bean Constructor To Pass In Message
Jsp code
<HTML>
<HEAD>
<TITLE>Using a JavaBean</TITLE>
</HEAD>
<BODY>
<h1>Using a JavaBean</h1>
<% beans.Test1 messager = new beans.Test1("message from"); %>
The message is: <%= messager.msg() %>
</BODY>
</HTML>
Call Setter In Java Bean To Change its Property Value
Jsp code
<HTML>
<HEAD>
<TITLE>Using Beans and Page Scope</TITLE>
</HEAD>
<BODY>
<H1>Using Beans and Page Scope</H1>
<jsp:useBean id="bean1" class="beans.Counter" scope="page" />
<%
bean1.setCounter(bean1.getCounter() + 1);
%>
The counter value is: <jsp:getProperty name="bean1" property="counter" />
</BODY>
</HTML>
Fields of the Calendar Class
<%@ page import="java.util.*" %>
<HTML>
<HEAD>
<TITLE>Fields of the Calendar Class</TITLE>
</HEAD>
<BODY>
<H1>Fields of the Calendar Class</H1>
<%
String dateString = new String();
GregorianCalendar calendar = new GregorianCalendar();
Date date1 = new Date();
calendar.setTime(date1);
dateString += "Calendar.YEAR is " + calendar.get(Calendar.YEAR) + "<BR>";
dateString += "Calendar.MONTH is " + calendar.get(Calendar.MONTH) + "<BR>";
dateString += "Calendar.WEEK_OF_YEAR is " + calendar.get(Calendar.WEEK_OF_YEAR) +
"<BR>";
dateString += "Calendar.WEEK_OF_MONTH is " + calendar.get(Calendar.WEEK_OF_MONTH)
+ "<BR>";
dateString += "Calendar.DATE is " + calendar.get(Calendar.DATE) + "<BR>";
dateString += "Calendar.DAY_OF_MONTH is " + calendar.get(Calendar.DAY_OF_MONTH) +
"<BR>";
dateString += "Calendar.DAY_OF_YEAR is " + calendar.get(Calendar.DAY_OF_YEAR) +
"<BR>";
dateString += "Calendar.DAY_OF_WEEK is " + calendar.get(Calendar.DAY_OF_WEEK) +
"<BR>";
dateString += "Calendar.DAY_OF_WEEK_IN_MONTH is "
+ calendar.get(Calendar.DAY_OF_WEEK_IN_MONTH) + "<BR>";
dateString += "Calendar.AM_PM is " + calendar.get(Calendar.AM_PM) + "<BR>";
dateString += "Calendar.HOUR is " + calendar.get(Calendar.HOUR) + "<BR>";
dateString += "Calendar.HOUR_OF_DAY is " + calendar.get(Calendar.HOUR_OF_DAY) +
"<BR>";
dateString += "Calendar.MINUTE is " + calendar.get(Calendar.MINUTE) + "<BR>";
dateString += "Calendar.SECOND is " + calendar.get(Calendar.SECOND) + "<BR>";
dateString += "Calendar.MILLISECOND is " + calendar.get(Calendar.MILLISECOND) +
"<BR>";
dateString += "Resetting the date!<BR>";
calendar.set(2005, 11, 31, 23, 59);
dateString += "Calendar.YEAR is " + calendar.get(Calendar.YEAR) + "<BR>";
dateString += "Calendar.MONTH is " + calendar.get(Calendar.MONTH) + "<BR>";
dateString += "Calendar.WEEK_OF_YEAR is " + calendar.get(Calendar.WEEK_OF_YEAR) +
"<BR>";
dateString += "Calendar.WEEK_OF_MONTH is " + calendar.get(Calendar.WEEK_OF_MONTH)
+ "<BR>";
dateString += "Calendar.DATE is " + calendar.get(Calendar.DATE) + "<BR>";
dateString += "Calendar.DAY_OF_MONTH is " + calendar.get(Calendar.DAY_OF_MONTH) +
"<BR>";
dateString += "Calendar.DAY_OF_YEAR is " + calendar.get(Calendar.DAY_OF_YEAR) +
"<BR>";
dateString += "Calendar.DAY_OF_WEEK is " + calendar.get(Calendar.DAY_OF_WEEK) +
"<BR>";
dateString += "Calendar.DAY_OF_WEEK_IN_MONTH is "
+ calendar.get(Calendar.DAY_OF_WEEK_IN_MONTH) + "<BR>";
dateString += "Calendar.AM_PM is " + calendar.get(Calendar.AM_PM) + "<BR>";
dateString += "Calendar.HOUR is " + calendar.get(Calendar.HOUR) + "<BR>";
dateString += "Calendar.HOUR_OF_DAY is " + calendar.get(Calendar.HOUR_OF_DAY) +
"<BR>";
dateString += "Calendar.MINUTE is " + calendar.get(Calendar.MINUTE) + "<BR>";
dateString += "Calendar.SECOND is " + calendar.get(Calendar.SECOND) + "<BR>";
dateString += "Calendar.MILLISECOND is " + calendar.get(Calendar.MILLISECOND) +
"<BR>";
out.println(dateString);
%>
</BODY>
</HTML>
Get/Set Value Using JSP Set Property
Jsp code
<HTML>
<HEAD>
<TITLE>Getting a Property Value</TITLE>
</HEAD>
<BODY>
<H1>Getting a Property Value</H1>
<jsp:useBean id="bean1" class="beans.Test4" />
The message is: <jsp:getProperty name="bean1" property="message" />
<BR>
<jsp:setProperty name="bean1" property="message" value="Hello again!" />
Now the message is: <jsp:getProperty name="bean1" property="message" />
</body>
</html>
Import JavaBeans In JSP Page
Jsp page
<%@ page import="beans.Test" %>
<HTML>
<HEAD>
<TITLE>Using a JavaBean</TITLE>
</HEAD>
<BODY>
<h1>Using a JavaBean</h1>
<% Test messager = new Test(); %>
The message is: <%= messager.msg() %>
</BODY>
</HTML>
JSP Helper Bean HTMLFilter
Jsp code
<html>
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<body bgcolor="white">
<h1> Request Information </h1>
<font size="4">
JSP Request Method: <% out.print(beans.HTMLFilter.filter(request.getMethod())); %>
<br>
Request URI: <%= request.getRequestURI() %>
<br>
Request Protocol: <%= request.getProtocol() %>
<br>
Servlet path: <%= request.getServletPath() %>
<br>
Path info: <% out.print(beans.HTMLFilter.filter(request.getPathInfo())); %>
<br>
Query string: <% out.print(beans.HTMLFilter.filter(request.getQueryString())); %>
<br>
Content length: <%= request.getContentLength() %>
<br>
Content type: <% out.print(beans.HTMLFilter.filter(request.getContentType())); %>
<br>
Server name: <%= request.getServerName() %>
<br>
Server port: <%= request.getServerPort() %>
<br>
Remote user: <%= request.getRemoteUser() %>
<br>
Remote address: <%= request.getRemoteAddr() %>
<br>
Remote host: <%= request.getRemoteHost() %>
<br>
Authorization scheme: <%= request.getAuthType() %>
<br>
Locale: <%= request.getLocale() %>
<hr>
The browser you are using is <% out.print(beans.HTMLFilter.filter(request.getHeader("User-Agent"))); %>
<hr>
</font>
</body>
</html>
JSP Number Guess Bean
Jsp code
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Number Guess Game
Written by Jason Hunter, CTO, K&A Software
http://www.servlets.ru
-->
<%@ page import = "beans.*" %>
<jsp:useBean id="numguess" class="beans.MyBean" scope="session"/>
<jsp:setProperty name="numguess" property="*"/>
<html>
<head><title>Number Guess</title></head>
<body bgcolor="white">
<font size=4>
<% if (numguess.getSuccess()) { %>
Congratulations! You got it.
And after just <%= numguess.getNumGuesses() %> tries.<P>
<% numguess.reset(); %>
Care to
== Load JavaBeans In JSP Page ==
<p>Test.java</p>
<!-- start source code -->
<source lang="java">
package beans;
public class Test
{
public Test()
{
}
public String msg()
{
return "Hello from JSP!";
}
}
Reference Calendar Bean
Jsp code
<html>
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<%@ page session="false"%>
<body bgcolor="white">
<jsp:useBean id="clock" scope="page" class="beans.MyBean" type="beans.MyBean" />
<font size=4>
<ul>
<li> Day of month: is <jsp:getProperty name="clock" property="dayOfMonth"/>
<li> Year: is <jsp:getProperty name="clock" property="year"/>
<li> Month: is <jsp:getProperty name="clock" property="month"/>
<li> Time: is <jsp:getProperty name="clock" property="time"/>
<li> Date: is <jsp:getProperty name="clock" property="date"/>
<li> Day: is <jsp:getProperty name="clock" property="day"/>
<li> Day Of Year: is <jsp:getProperty name="clock" property="dayOfYear"/>
<li> Week Of Year: is <jsp:getProperty name="clock" property="weekOfYear"/>
<li> era: is <jsp:getProperty name="clock" property="era"/>
<li> DST Offset: is <jsp:getProperty name="clock" property="DSTOffset"/>
<li> Zone Offset: is <jsp:getProperty name="clock" property="zoneOffset"/>
</ul>
</font>
</body>
</html>
Reference Package Name in JSP Page
Test.java
package beans;
public class Test
{
public Test()
{
}
public String msg()
{
return "Hello from JSP!";
}
}
Using initialized field in jsp page
Jsp page code
<HTML>
<HEAD>
<TITLE>Using <jsp:useBean></TITLE>
</HEAD>
<BODY>
<H1>Using <jsp:useBean></H1>
<jsp:useBean id="bean1" class="beans.Test3" />
The message is: <%= bean1.getMessage() %>
</BODY>
</HTML>
Using the Date Class
<HTML>
<HEAD>
<TITLE>Using the Date Class</TITLE>
</HEAD>
<BODY>
<H1>Using the Date Class</H1>
The date is: <%= new java.util.Date() %>.
</BODY>
</HTML>
Using "useBean" To Reference A Java Bean
Jsp page
<HTML>
<HEAD>
<TITLE>Using <jsp:useBean></TITLE>
</HEAD>
<BODY>
<H1>Using <jsp:useBean></H1>
<jsp:useBean id="bean1" class="beans.Test2" />
The message is: <%= bean1.msg() %>
</BODY>
</HTML>