jsp页面语法

jsp页面语法

1JSP项目目录:webapps(页面 +WEB-INF目录)》WEB-INF(web.xml+classes目录)


2.<table width="200" border="1">

 <tr bgcolor=33ffcc> <td>姓名</td>  <td>id</td>   </tr>  </table>


3.对复选框的的参数处理:String s[ ]=request.getParameterValues("fuxuan");

if(s!=null){out.print("复选框");for(int i=0;i<s.length;i++){out.print(s[i]+",");}}

  getParameterValues只是对多个名字的属性值以数组形式储存


4.JSP脚本元素:a..注释:<%--客户端看不见--%>  < --客户端看可见且可插表达式-->

                 b.声明,在编译时可以被class识别为变量<%!  %>

                 c.表达式:<%=new java.util.Date().to.LocaleString() %> 就不用带;

                 d.Scriptlet:<% 包含jsp语句,方法,变量以及表达式 %>


5.jsp指令元素:用来定义JSP文件中的全局属性,除import外其他指令只在第一次有效。

a.<%@  pagecontentType="text/html; charset=gb2312"     language="java"

import="java.sql.*", import="java.io.*" errorPage="error.jsp" buffer="12kb"  autoflush="true"info="这是一个页面设置 isErrorpage="false" isThreadSafe=true  %>

buffer的大小被out对象用于缓存处理执行后的jsp对客户浏览器的输出,默认为8kb.

autoFlush用来设置buffer溢出时是否需要强制输出。默认为true

若buffer 为none,auto flush必须为true; 没有缓存,只有溢出

b.include指令元素: <%@ include file="head.jsp"   %>  <%@  include file="body.htm" %>


6.Jsp动作指令


a.Include 指令:
包含的内容是动态改变的,在执行期时才确定(在调用时才包含)

//static.htm   <form name="form1" method="post" action="jsp_include.jsp"> </form>

<%@ include file="static.htm"%>

<a href="two.jsp?param1=1&param2=2">goto two</a><br>//静态传递是固定的参数数值

include works

<jsp:include page="two.jsp" flush="true">

<jsp:param name="param1" value="<%=request.getParameter("name")%>"/>

<jsp:param name="param2" value="<%=request.getParameter("password")%>"/>

</jsp:include>                用name的参数名进行动态传递

//two.jsp   this is a1=<%=request.getParameter("param1")%><br>

this is a2=<%=request.getParameter("param2")%>


b. Forward 指令:在同一个项目内使用跳转,转向。

<% String userId=request.gsetParameter("userId");

String password=request.getParameter("password");if(userId.equals("hello")){%>

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

<jsp:param name="userId" value="<%=userId%>" ></jsp:param>

<jsp:param name="password" value="<%= password %>"></jsp:param>   

</jsp:forward>          <% }else { %>  <jsp:forward page="error.jsp"/>  <% } %>

//success.jsp    欢迎您:<%= request.getParameter("userId") %>

你的登陆密码是:<%= request.getParameter("password") %>

    

c.useBean 指令:
在Bean中的属性名,类型必须和request对象的参数名称相同(表单传来的都是String类型,JSP 内在机制自动转换为Bean属性类型),如果request参数为空,则Bean属性不设置值。Javabean getProperty/setProperty的getter/setter具有自动机制

package com.jsp.ch2;

public class Person{ private String name;private String sex;private String password;

private int age;     public void setName(String n){name=n;}

public void setSex(String s){sex=s;}   public void setPassword(String p){password=p;}

public void setAge(int a){age=a;}

 

public String getName( ){return name;}   public String getSex(){return sex;}

public String getPassword(){return password;}  public int getAge(){return age;}}

<jsp:useBean id="person" scope="page" class="com.jsp.ch2.Person"> //new了一个person对象 <jsp:setProperty name="person" property="*"></jsp:setProperty>  </jsp:useBean>

//name相当于对象名,property相当于成员名,用setProperty进行set关联

<% request.setCharacterEncoding("gb2312"); %>

用户名:<%= person.getName() %>    性别:<%= person.getSex() %><br>

密码:<%= person.getPassword() %>  年龄:<%= person.getAge() %><br>

使用getProperty()方法:用户名:<jsp:getProperty name="person" property="name" />

// getProperty()调用get方法:性别:<jsp:getProperty name="person" property="sex" />

密码:<jsp:getProperty name="person" property="password" />

年龄:<jsp:getProperty name="person" property="age" />



7.JSP内置对象:

a.Session的会话:

<% String name=request.getParameter("name");

String password=request.getParameter("password");

String type=request.getParameter("type");

if(name.equals("nihao")&&password.equals("nihao")){

session.setAttribute("name1",name);//将name对象的值添加进去,且取键,通过键获对象

session.setAttribute("type",type); response.sendRedirect("loginsucess.jsp");}

else {response.sendRedirect("session_login.htm");}%>

取回session:

登陆成功,欢迎您!<%= session.getAttribute("name1") %>

<% if(session.getAttribute("type").equals("manager"))  {  %>  进入管理页面

   <% } else { %>  进入使用界面  <% } %>


c
Exception对象

<%@ page contentType="text/html; charset=gb2312" language="java"

import="java.sql.*" import="java.io.*" iserrorPage="True" %>

<%= exception.getMessage()%>

<%exception.printStackTrace(new PrintWriter(out));  %>//内部已经有打印输出

总结:a.getAttribute返回Object类型的对象,getParameter返回String类型的对象

request虽然只能在两个页面之间传递,但是可以通过request.setattribute传递到第三方页面。

b.getParameter传递的数据会从客户端传到web服务器端,代表http请求数据

getAttribute/setAttribute方法传递的数据只会存在于web容器内部,在具有转发关系的web组件之间传播

session.setattribute与request.getattribute之间是无法取得值的

page:当前页面范围;request:在一个jsp发出请求到另一个jsp网页之间;session:一段用户和服务器连接的时间;application:在服务器一开始启动到关闭为止。


8: request在页面间范围存在,不用创建新的了,范围正好才行:

javabean: package mypack;

public class CounterBean{  private int count=0;  public CounterBean(){} 

  public int getCount(){  return count;} 

  public void setCount(int count){  this.count=count;  }  }

page: <%@ page import="mypack.CounterBean"  %>     //导入才能用类的类型定义

<jsp:useBean id="myBean" scope="request" class="mypack.CounterBean"/>

Current count value is:<jsp:getProperty name="myBean" property="count" />

<jsp:setProperty name="myBean" property="count"

value="<%= myBean.getCount()+1 %>"/>   //此处设置了对象属性名供get取出

<% CounterBean obj=null;  String scope=null;

  obj=(CounterBean)request.getAttribute("myBean");  if(obj!=null)scope="request";

  obj=(CounterBean)session.getAttribute("myBean");  if(obj!=null)scope="session";

  obj=(CounterBean)application.getAttribute("myBean");//将对象取出来

if(obj!=null)scope="application";

  if(scope==null) scope="page";%> scope=<%= scope %>

<jsp:forward page="requestCounter1.jsp" />  //在url路径信息是不显示的,只是调用了使用过程requestCounter1.jsp:

<jsp:useBean id="myBean" scope="request" class="mypack.CounterBean"/>

Current count value is:<jsp:getProperty name="myBean" property="count" />

session  对象是永远存在的,只要用户存在。application.getAttribute();也是有的。



9.JDBC:JAVA程序与数据库系统通信的标准API(与数据库进行连接,发送SQL语句,处理结果)

常用的JDBC类与方法:

a.Mysql:Class类:返回与带有给定字符串名的类或接口相关联的Class对象

<% try{

Class.forName("com.mysql.jdbc.Driver");//一.加载驱动程序,在lib下由sun提供

String url="jdbc:mysql://127.0.0.1:3306/mydb";   String user="root";

String password="root";

Connection conn=DriverManager.getConnection(url,user,password);

//二.Connection负责维护JSP/JAVA数据库程序和数据库之间的联机, DriverManager 类会尝试加载在 "jdbc.drivers" 系统属性中引用的合适的驱动程序类.

 

Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,

ResultSet.CONCUR_UPDATABLE);   //参数不写也行,默认自带,自动滚动和游标

//三.Connection类createStatement方法创建一个 Statement 对象将 SQL 语句发送到数据库

String sql="select * from mytable"; //stmt.executeQuery(sql);//四.执行

     

ResultSet rs=stmt.executeQuery(sql);// executeQuery是Statement类方法

while(rs.next()){out.println("first "+rs.getInt("age"));//返回结果集在记录表最上面

     out.println("second "+rs.getString("name")+"<br>");    }

     out.print("success!");rs.close();stmt.close();conn.close();    }

     catch (Exception e){e.printStackTrace();  } %>

//五.取的执行结果,用Statement来执行sql语句,该语句返回单个 ResultSet 对象。

Statement sm=cn.createStatement();sm.executeQuery(sql);sm.executeUpdate(sql);


b.预处理语句:<% try{ PreparedStatement pstmt=conn.prepareStatement(

"insert into userinfo values(?,?)");

pstmt.setString(1,"nihao"); pstmt.setInt(2,3232);  pstmt.execute();  pstmt.close();

out.print("success!");pstmt.close();  con.close()}    catch (Exception e){ } %>

你可能感兴趣的:(jsp页面语法)