黑马程序员_java_JSP

1.JSP(java Server Page)页结构

jsp页面中主要包含两种元素:标签和代码。标签主要有指令标签和动作标签,代码主要有java代码、jsp声明、jsp表达式。

嵌入在jsp页面中的java代码分为两种:一种是jsp页面中一些变量和方法的声明 ,使用<!%和%>标记;另一种是用<%%>来包含java代码块。

静态指令:使用的语法:

<%@ includefile="要插入的文件名" %>

JSP程序注释

语法:
1。<%-- JSP程序的注释内容 --%> 。若要显示不两侧的--%>,可以使用--%\>。另外java的代码也可以使用//注释。

2。另外一种客户端嵌入注释

这种方式服务器会把代码传到客户端,在客户端注释。也就是说会在窗户客显示已注释的代码。

JSP的内置对象

jsp有9种常用的内置对象,常用的有request,response,session,appplication,out.

request     请求,代表来自客户端的请求。

 常用的方法 :String getparameter(String name)  获得表单中提交的数据   

response     响应, 代表来自服务端的响应。
 常用的方法 :1.定向 response.encodeURL("网页");
    2.response.encodeRedirectURL("网页");
    设置页面自动刷新
    <%response.setHeader("Refresh","5");设置页面每隔5秒自动刷新
    %>
session    session以name/value的形式存储一些对象类型的信息

 

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%request.setCharacterEncoding("utf-8");//将网页接收的字符的编码设置为UTF-8
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>


<html>
<head>
<base href="<%=basePath%>">

<title>My JSP 'MyJsp.jsp' starting page</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
 <link rel="stylesheet" type="text/css" href="styles.css">
 -->

</head>

<body>
 <%
  String userName=request.getParameter("userName");//使用java代码取得想到得到的数据
 
String pSd=request.getParameter("pSd");//取得客户端返回的name为pSd的内容
%>

 你好,<%=userName%>,欢迎光临我的网站。你刚才输入的密码为<%=pSd%>//标签,使用java代码
 <% if(pSd=="123"){
out.println("登录成功");//out对象的使用
}%>

 

</body>
</html>

  

posted on 2012-09-26 23:08  晚風輕揚  阅读(99)  评论(0)    收藏  举报

导航