==Jsp==:java sever pages
翻译 编译 执行
jsp可以重用编译好的Java源码,提高速度。
- 注释:
指令:
<%@ page language="java" import="java.util.*,java.text.*" contentType="text/html; charset=GBK" %>
脚本:
<%
int numA = 4, numB = 5 ;
int result = numA+numB;
%>
表达式 打印输出:
<%=result %>
方法体
<%!
String formatDate(Date d){
SimpleDateFormat formater = new SimpleDateFormat("yyyy年MM月dd日");
retrun formater.format(d);
}
%>
<%=formatDate(new Date()) %>
<%--方法可以在页面多处调用--%>
==Jsp内置对象==
- out session request application response
- ==requsert==:用于处理客户端请求。
request对象方法 | 用处 |
---|---|
getParameter | 单个数据 |
ParameterValues | 对应多个值时 |
setCharacterEncoding | 请求的乱码 |
getRemoterAddr() | 返回客户机地址 |
注册login.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'test04.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>
<form name="form1" method="post" action="reginfo.jsp">
<table border="0" align="center">
<tr> <td>用户名</td><td> <input type="text" name="name"></td></tr>
<tr><td>密码</td><td > <input type="password" name="pwd"> </td></tr>
<tr> <td>您的信息来源</td><td>
<input type="checkbox" name="channel" value="报刊">报刊
<input type="checkbox" name="channel" value="网络">网络
<input type="checkbox" name="channel" value="朋友推荐"> 朋友推荐
<input type="checkbox" name="channel" value="电视"> 电视
</td></tr>
<!-- 以下是提交、取消按钮 -->
<input type="submit" value="提交">
</table>
</form>
</body>
</html>
信息读取reginfo.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'reginfo.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>
This is my JSP page.
<br>
<%
request.setCharacterEncoding("utf-8");
//读取用户名和密码
String name = request.getParameter("name");
String pwd = request.getParameter("pwd");
%>
用户名:<%=name%><br/>
密码:<%=pwd%><br/>
信息来源:
<%
//读取复选框选择项
String[] channels = request.getParameterValues("channel");
if (channels != null) {
for (int i = 0; i < channels.length; i++) {
out.print("<br/>"+channels[i]);
}
}
%>
</body>
</html>
乱码
<% ----以POST方式提交数据时----
//设置读取请求信息的字符编码为GBK或者GB2312
request.setCharacterEncoding("GBK");
//读取用户名和密码
String name = request.getParameter("name");
String pwd = request.getParameter("pwd");
%>