@nalan90
2018-06-29T11:16:39.000000Z
字数 8190
阅读 533
JAVAEE开发
<%
int[] value = {60, 70, 80};
for (int i: value) {
out.println(i);
}
%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>out对象</title>
</head>
<body>
<%
out.println("<h2>静夜思</h2>");
out.println("床前明月光<br>");
out.println("疑是地上霜<br>");
out.flush();
//out.clear(); //会抛出异常
out.println("举名望明月<br>");
out.println("低头思故乡<br>");
%>
buffer size: <%=out.getBufferSize()%> bytes<br>
remain buffer size: <%=out.getRemaining()%> bytes<br>
auto clear buffer: <%=out.isAutoFlush()%><br>
</body>
</html>
## reg.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>注册</title>
</head>
<body>
<h1>用户登录</h1>
<hr>
<form action="request.jsp" name="loginForm" method="post">
用户名: <input type="text" name="username"><br>
爱好: <input type="checkbox" name="fav" value="read">读书
<input type="checkbox" name="fav" value="music">音乐
<input type="checkbox" name="fav" value="movie">电影
<input type="checkbox" name="fav" value="swimming">游泳
<br>
<input type="submit" value="login">
</form>
<br>
<br>
<a href="request.jsp?username=张爽">测试URL传参</a>
</body>
</html>
##request.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>注册主页</title>
</head>
<body>
<h1>request内置对象</h1>
<%
//解决中文乱码问题,无法解决url传递的中文乱码
request.setCharacterEncoding("utf-8");
request.setAttribute("password", "123456");
%>
用户名: <%=request.getParameter("username")%>
<br>
爱好:
<%
if (request.getParameterValues("fav") != null) {
String[] favs = request.getParameterValues("fav");
for (String fav: favs) {
out.println(fav+" ");
}
}
%>
<br>
<%=request.getContextPath()%><br>
<%=request.getPathInfo()%><br>
<%=request.getServletPath()%><br>
<%=request.getContentType()%><br>
<%=request.getPathInfo()%><br>
<%=request.getProtocol()%><br>
<%=request.getServerName()%><br>
<%=request.getServerPort()%><br>
<%=request.getRemoteAddr()%><br>
<%=request.getAttribute("password")%>
</body>
</html>
注意:解决URL传递中文乱码问题如下:
1、编辑tomcat conf/server.xml文件,添加 URIEncoding="utf-8"
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" URIEncoding="utf-8"/>
2、重启tomcat
常用方法
请求转发与请求重定向
示例
<%@ page contentType="text/html;charset=UTF-8" import="java.io.*" language="java" %>
<%
response.setContentType("text/html;charset=utf-8");
out.println("<h1>response内置对象</h1>");
out.println("<hr>");
//out.flush();
PrintWriter pw = response.getWriter();
pw.println("大家好,我是response对象生成的输出流out对象");
//response.sendRedirect("reg.jsp");
//response.sendRedirect("request.jsp"); //请求重定向
request.getRequestDispatcher("request.jsp").forward(request, response); //请求转发
%>
什么是session
session的生命周期
常用方法
session设置超时
<session-config>
<session-timeout>1</session-timeout>
</session-config>
示例
##session.pageone.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="java.text.SimpleDateFormat" %>
<%@ page import="java.util.Date" %>
<html>
<head>
<title>session.pageone.jsp</title>
</head>
<body>
<h1>session对象</h1>
<%
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date d = new Date(session.getCreationTime());
session.setAttribute("username", "zhangshuang");
session.setAttribute("password", "123456");
session.setAttribute("age", "28");
//session.setMaxInactiveInterval(10);
%>
session创建时间: <%=sdf.format(d)%><br>
session的ID编号: <%=session.getId()%><br>
session中获取用户名: <%=session.getAttribute("username")%><br>
<%
//session.invalidate(); //销毁session
%>
<a href="session.pagetwo.jsp" type="_blank">跳转到session.pagetwo.jsp</a>
</body>
</html>
##session.pagetwo.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>session.pagetwo.jsp</title>
</head>
<body>
session的ID编号: <%=session.getId()%><br>
session中获取用户名: <%=session.getAttribute("username")%><br>
</body>
</html>
<%@ page import="java.util.Enumeration" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>application对象</title>
</head>
<body>
<%
application.setAttribute("city", "北京");
application.setAttribute("postcode", "100000");
application.setAttribute("email","admin@admin.com");
%>
城市:<%=application.getAttribute("city")%><br>
application所有属性名称:
<%
Enumeration attributes = application.getAttributeNames();
while (attributes.hasMoreElements()) {
out.println(attributes.nextElement()+"<br>");
}
%>
<br>
JSP引擎信息<%=application.getServerInfo()%>
</body>
</html>
## login.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>登录页面</title>
</head>
<body>
<h1>用户登录</h1>
<hr>
<form action="dologin.jsp" name="loginForm" method="post">
用户名: <input type="text" name="username"><br>
密码:<input type="password" name="password"><br>
<input type="submit" value="login">
</form>
</body>
</html>
## dologin.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
request.setCharacterEncoding("utf-8");
String username = "";
String password = "";
username = request.getParameter("username");
password = request.getParameter("password");
if ("admin".equals(username) && "admin".equals(password)) {
session.setAttribute("loginUser", username);
request.getRequestDispatcher("login_success.jsp").forward(request, response);
} else {
response.sendRedirect("login_fail.jsp");
}
%>
##login_success.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>登录成功</title>
</head>
<body>
<%
String loginUser = "";
if (session.getAttribute("loginUser") != null) {
loginUser = session.getAttribute("loginUser").toString();
}
%>
欢迎您<%=loginUser%>
</body>
</html>
##login_fail.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>登录失败</title>
</head>
<body>
<h3>登录失败</h3>
<a href="login.jsp">重新登录</a>
</body>
</html>