java学习笔记jsp基础1

1.HelloWorld.html 可以通过ie直接访问(打开)
2.HelloWorld.jsp  放在WebRoot目录下,可以通过ie访问 http://localhost:8080/jsp_example/helloworld.jsp
  在<%=  %>字符系列之间嵌入Java表达式,且用于输出Java表达式;
  在<%   %>字符系列之间嵌入Java代码,如果需要输出时用 <%out.print("user.home" + str1); %> 进行输出。
exp:
<HTML>
<BODY>
Hello! The time is now <%= new java.util.Date() %><br>           
java.version  <%= System.getProperty("java.version") %><br>
java.home  <%= System.getProperty("java.home") %><br>
os.name  <%= System.getProperty("os.name") %><br>
<% String str1=System.getProperty("user.home"); String str2=System.getProperty("user.dir");%><br>
<%out.println("user.home" + str1); %><br>                         //此处out可以产生html输出,println没有换行功能(与print没有区别),通过<br>换行
<% System.out.println( "Evaluating date now" );                   //此处不会显示在浏览器上,而是显示在Console视图(服务器日志)上,可用于调试
   java.util.Date date = new java.util.Date();%>
 Hello! The time is now <%= date %>
<table border="2">                   //表格边框位2
 <% int n=10;
  for (int i=0;i<n;i++) { %>
  <TR>
    <TD>Number</TD>                 //2列10行的表格
    <TD><%= i+1 %></TD>
  </TR>
 <% } %>
 </table>
</BODY>
</HTML>

exp2:
<%@ page import="java.util.*" %>   //page directive
<HTML>
<BODY>
<%!                                //声明变量和方法   <%! declaration; [ declaration; ]+ ... %>
Date theDate = new Date();
Date getDate()                     //getDate()只能返回一次固定的时间
{
System.out.println( "In getDate() method" );
return theDate;
}
Date computeDate()                 //computeDate()可以返回变动的时间
{
 theDate=new Date();
 System.out.println( "In computeDate() method" );
 return theDate;
}
%>
<!-- Hello! The time is now <%= getDate() %>  -->   //注释
Hello! The time is now <%= computeDate() %>
<jsp:include page="helloworld.jsp"/>        //显示本页面与helloworld.jsp页面的信息
<jsp:forward page="helloworld.jsp"/>        //仅显示helloworld.jsp页面的信息

</BODY>
</HTML>

注释:
<!-- comment [ <%= expression %> ] --> 可以使用表达式,且在“查看源码”中可以看到; <%-- comment --%>  不能使用表达式,且不能在“查看源码”中可看到。

"page directive"可以包含所有的引入的项目。引入多个项目可以用逗号(,)来分隔项目,如 :<%@ page import="java.util.*,java.text.*" %>
标志可以分成两种类型:一种是从外部标志库中转载的,另外一种是预先定义的标志。预先定义的标志是以jsp:字符开始的,如:jsp:include,jsp:forward
标志语法1:
<some:tag>
body
</some:tag>
标志语法2:
<some:tag/>

Session是一个跟用户相关的对象。当用户访问网站的时候,一些数据就被存放于session中,并在需要的时候从中取出数据。Session为不同的用户保存了不同了数据。
<%
String name = request.getParameter( "username" );  //用request获取界面属性
session.setAttribute( "theName", name );           //将获取到的界面属性存在session对象中,给定一个键"theName"
%>
<%= session.getAttribute( "theName" ) %>           //输出session对象中键"theName"对应的键值

一个声明仅在一个页面中有效。如果你想每个页面都用到一些声明,最好把它们写成一个单独的文件,然后用<%@ include %>或<jsp:include >元素包含进来。

原文地址:https://www.cnblogs.com/BradMiller/p/1831987.html