Jsp----write Java code (variables and function Methods) in the JSP

Source: Internet
Author: User

<% @page import= "java.text.SimpleDateFormat"%>
<% @page language= "java" import= "java.util.*, java.awt.*" errorpage= "jsps/error.jsp" pageencoding= "UTF-8"%>
<!--jsp in the Guide package, see above (two ways) jsps/error.jsp set a JSPs folder to write a error.jsp page--

<% @taglib uri= "http://java.sun.com/jsp/jstl/core" prefix= "c"%>

<! DOCTYPE HTML public "-//w3c//dtd HTML 4.01 transitional//en" >
<title>jsp Technology Demo </title>

<body>
<%

Button btn = New button ("test Guide package");
Date d = new Date ();
SimpleDateFormat sdf = new SimpleDateFormat ("yyyy-mm-dd HH:mm:ss");
Out.println (sdf.format (d));

Local variables---in JSP are valid within the underlying function _jspservice ()
String str= "hunan City courtyard";


Pagecontext.setattribute ("name", "page1111");
Request.setattribute ("name", "req22222");
Session.setattribute ("name", "session33333");
Application.setattribute ("name", "app44444");
%>


<%! A METHOD----A member variable or method in a JSP that is Index_jsp.java and _jspservice ()
String Hello () {
Return "hello";
}
%>
<br/>
<c:foreach begin= "0" end= "ten" >
Random output point information ...<br/>
</c:forEach>

<%
Out.println (str); Accessing local variables
OUT.PRINTLN (hello ());//access class member
Exception.getstacktrace (); With exception, You must add the attribute in the page directive: iserrorpage= "true"

string res = (string) Pagecontext.findattribute ("name");//from: pagecontext,request,session,applicat These 4 containers from small to large to read the properties
Out.println (res);

String res2 = (string) Pagecontext.findattribute ("name2");//if The attribute is not in 4 containers, the output: null
Out.println (res2);//null
%>
<br/>
ooooo:${name} &nbsp;&nbsp;&nbsp;
kkkkk:${name2} <!--if The property does not exist in 4 containers, Nothing is output (null is not output)--

<br/>

<!--fast output, Small details:%= Two next to each other, do not add ";" at the End. --
<%=str+ "2222"%>
<%=str+ "3333"%>

<%
The following is used to test the Erropage property
int i = 100/0;
%>

</body>

Jsp----write Java code (variables and function Methods) in the JSP

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.