javaweb开发

  • 输出日期:

<%

SimpleDateFormat dfs = new SimpleDateFormat("yyyy年MM月dd天HH:mm:ss");

String s = dsf.format(new Date());

%>

今天的日期是:<%= s %>

  • request对象

javaweb开发

javaweb开发

response的getWriter()比out.print()要提前输出

response.sendRedirect();跳转

 request.getRequestDispatcher("respone.jsp").forward(request,response);请求转发,不向客户端要求重新发送请求,直接转发给respone.jsp

  • session对象

 

javaweb开发

设置session最长有效时间        session.setMaxInactiveInterval(1);

  • application对象

javaweb开发

    

<%

/*

遍历所有的属性对象

*/
        Enumeration enumt= application.getAttributeNames();
        while(enumt.hasMoreElements()){
            out.println(enumt.nextElement()+"<br/>"+application.getAttribute(enumt.nextElement().toString()));
        }
    %>
    <%=

//获取版本号
    application.getServerInfo()
     %>

  • page对象

page对象是指向当前页面本身,类似this指针。

<%@  erroPage="action.jsp"%>出现异常后跳转页面

<%@ isErrorPage="true"%>目标页面需要设置为是处理异常页面

javaweb开发

  • pageContext对象

javaweb开发

  • config对象

javaweb开发

  • Exception对象

javaweb开发

  • javabeans

实例化javabean

javaweb开发

<jsp:useBean id="user" class="beans.User" scope="page" />
    用户名:<%= user.getUsername() %>
    密码&nbsp;:<%= user.getPsw() %>

  •     jsp:setPropertyjavaweb开发

<jsp:useBean id="user" class="beans.User" scope="page" />
<jsp:setProperty property="*" name="user"/>
    用户名:<%= user.getUsername() %>
    <hr/>
    密码&nbsp;:<%= user.getPsw() %>

相当于自动收集表单

  • jsp:getProperty

类似getName()成员函数,通常用户获取javabean类的私有变量

  • scope设置javabean的作用范围

javaweb开发

application相当于整个网站共有,类似有多少人浏览这样的功能。

session只是当前这次访问才会有效,类似查看用户是否在线

request只有当用户请求才会有效,类似登录进行验证。

page只是在当前页面有效就算使用了站内转发也其他页面也不能获取。

  • 创建与使用cookie

javaweb开发

javaweb开发

使得cookie失效方法:

Cookie [] cookies = request.getCookies();

if(cookies !=null&&cookies.length>0)

 {

        for(Cookie c:cookies){

             c.setMaxAge(0);

           response.addCookie(c);

        }

 }

  • include指令

<%@ include  file="文件路径" %>

通常用于在页面中插入一段其他页面的代码

include动作

<jsp:include page="文件路径" flush="false" >

include指令相当于直接包含源代码

include动作是包含的是原文件运行后的页面

  • jsp:forward动作

javaweb开发

  • jsp:param动作

javaweb开发

例如:

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

<jsp:param value="[email protected]" name ="email"/>

</jsp:forward>

相当于我们新增了一个属性email在user.jsp可以多接收一个参数。

  • java连接数据的实现

   private String driver ="com.mysql.jdbc.Driver";//加载mysql驱动
   //连接链接
   private String url = "jdbc:mysql://localhost:3306/shop?useUnicode=true&characterEncoding=UTF";
   private String name = "root";//数据库用户名
   private String password = "123";//数据库密码