一。什么是jsp:
1.只能运行在服务器中
2.可以将java代码嵌入html页面中的技术
补充:在eclipse中修改jsp的创建模板
window--preference--web--jsp file--editors--templates--new jsp file(html)--editor
B/S的开发模式:浏览器/服务器
二。语法:
1.指令:
<%@ page %>:是指明当前网页的一些基本要素,比如说字符集
<%@ include %>:是要引入另外一个网页资源
<%@ taglib %>:引入标签
2.java脚本
<% java代码 %>:这个java脚本可以嵌入在html的任何地方
例如:
<%
if(1==2){
%>
<p>你们看的见我吗?</p>
<%
}
%>
3.java的输出表达式
<%=表达式%>
4.注释:
<!-- -->:普通xml注释,客户端可见
<%-- -->:jsp注释,客户端不可见
5.jsp的内置对象:9大内置对象,是在jsp的java代码中直接使用,不需要声明和初始化,也不能作为其他变量名
a.request:请求 通常是用来获取页面表单传递到后台的数据
1.传参方式url:http://127.0.0.1:8080/web06/xxx.jsp?username=username&passowrd=123
注意:如果是中文参数需要转码后才能传递
String username = URLEncoder.encode("管理员","utf-8");//将中文加密成unicode
2.表单传参:可以传字符串还可以文件
注意:表单有两种方式提交数据get和post,区别:get会在url后显示参数和值,post不会,get方式不能提交文件,post可以
中文乱码问题:可以在获取参数之前调用request.setCharacterEncoding("utf-8");
如果是复选框传参,后台获取参数的方法是String strs[] = request.getParameterValues("复选框的名字");
b.response:响应
c.session:会话
d.application:应用全局域
f.pageContxt:当前页面域
实例1:发送与接收参数
send.jsp
<%@page import="java.net.URLEncoder"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE html>
<html>
<head>
<base href="<%=basePath%>"/>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<%
String username = URLEncoder.encode("管理员","utf-8");//将中文加密成unicode
%>
<body>
1.URL传参<br>
<a href="jsp/accept.jsp?username=<%=username %>&password=123456">url传参数</a>
</body>
</html>
accept.jsp
<%@page import="java.net.URLDecoder"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE html>
<html>
<head>
<base href="<%=basePath%>"/>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
request.setCharacterEncoding("utf-8");
String username = request.getParameter("username");
System.out.print(username);
String password = request.getParameter("password");
%>
username=<%=username %><br>
passowrd=<%=password %>
</body>
</html>
实例2:表单提交完成注册
register.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE html>
<html>
<head>
<base href="<%=basePath%>"/>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<div align="center">
<h1>注册页面</h1>
<form action="jsp/success.jsp" method="post">
用户名:<input name="username"><br>
性别:男<input type="radio" value="男" name="sex" checked="checked">
女<input type="radio" value="女" name="sex"><br>
爱好:篮球<input type="checkbox" name="hobby" value="篮球">
音乐<input type="checkbox" name="hobby" value="音乐">
美女<input type="checkbox" name="hobby" value="美女">
LOL<input type="checkbox" name="hobby" value="LOL"><br>
职业:<select name="job">
<option>老师</option>
<option>军人</option>
<option>医生</option>
<option>律师</option>
</select><br>
说明:<textarea rows="5" cols="50" name="explain"></textarea><br>
<input type="submit" value="注册">
</form>
</div>
</body>
</html>
success.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE html>
<html>
<head>
<base href="<%=basePath%>"/>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<%
request.setCharacterEncoding("utf-8");
String username = request.getParameter("username");
String sex = request.getParameter("sex");
String job = request.getParameter("job");
String[] hobbys = request.getParameterValues("hobby");
String hobby="";
if(hobbys!=null)
for(int i=0;i<hobbys.length;i++){
if(i==hobbys.length-1){
hobby += hobbys[i];
}else{
hobby += hobbys[i]+",";
}
}
String explain = request.getParameter("explain");
%>
<body>
<div align="center">
<h1>恭喜注册成功!基本信息如下:</h1>
用户名:<%=username %><br>
性别:<%=sex %><br>
爱好:<%=hobby %><br>
职业:<%=job %><br>
说明:<%=explain %><br>
</div>
</body>
</html>