目录
1,application作用域
application:应用程序,jsp内置对象之一
作用:存储数据。
session:session失效(同一个会话里面超有有效,手动注销,会话关闭(浏览器关闭))
request:转发进行页面跳转
统计网站的在线用户
<%
/* Integer 是int的封装类
int是默认值是0;
Integer 的默认值是null;
*/
Integer count= 0;
//从application作用域里面获取当前的在线用户数量
Integer c=(Integer)application.getAttribute("count");
//如果用户数量不为null,赋予变量count
if(c !=null){//有数据
count = c;
}
count++;//把当前的用户加进去
//然后再把更改后的用户数量保存到application作用域里面
application.setAttribute("count", count);
%>
<%=application.getAttribute("count") %>在线用户
2,富文本编辑器
1,导入 ckeditor4插件
2,在页面引入ckeditor.js 文件
<script type="text/javascript" src="<%=request.getContextPath() %>/ckeditor4/ckeditor.js">
</script>
<body>
<div style="width: 90%">
<textarea rows="" cols="" cols="ckeditor" name="ckeditor1"></textarea>
</div>
</body>
下载网站:
3,文件上传
导入jar包
3.1.文件上传步骤
(1) 设置form表单的enctype属性:
<form enctype="multipart/form-data" method="post">。
(2)给文件选择标签添加name属性;
1.创建SmartUpload对象
2.设置SmartUpload对象的编码
3.(上传初始化)初始化SmartUpload对象,里面传的是pageContext对象
4.设置文件上传类型
5.设置文件不能上传类型
6.设置上传文件大小为50M
7.准备上传
8.得到第一个上传的文件
9.如果选择了文件
10.定义上传路径
11.开始上传文件(是上传到服务器里面)
public class TextServlet extends HttpServlet{
String path;//文件上传的路径
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//1,设置编码
resp.setContentType("text/html;chaeset=utf-8");
req.setCharacterEncoding("utf-8");
try {
//文件上传
//1.创建SmartUpload对象
SmartUpload su=new SmartUpload();
//2.设置SmartUpload对象的编码
su.setCharset("utf-8");
//3.(上传初始化)初始化SmartUpload对象,里面传的是pageContext对象
su.initialize(this.getServletConfig(),req,resp);
//4.设置文件上传类型
su.setAllowedFilesList("jpg,gif,png");
//5.设置文件不能上传类型
su.setDeniedFilesList("exe,mp3,avi,mp4");
//6.设置上传文件大小为50M
su.setMaxFileSize(1024*1024);//byte
//7.准备上传
su.upload();
//8.得到第一个上传的文件
File file = su.getFiles().getFile(0);
//9.判断是否选择了文件
if(!file.isMissing()) {//选择了文件Picture1.jpg
/**
* 文件路径中间的间隔符;
* windows;\
* linux;/
* windows;c:\text\sa.jsap
* linux;c:\text\sa.jsp
*/
//10.定义上传路径
path = "upload"+java.io.File.separator+file.getFieldName();
//11.开始上传文件(是上传到服务器里面)
file.saveAs(path);
//把上传的图片路径path保存到session作用域里面
req.getSession().setAttribute("path", path);
//在使用了文件上传之后,要用smartupload里面的request对象获取表单提交的其它数据
Request request = su.getRequest();
//获取form表单里面提交的其它数据
String str = request.getParameter("ckeditor1");
System.out.println("str = " + str);
// 跳回到index页面
resp.sendRedirect(req.getContextPath()+"/index.jsp");
}
} catch (Exception e) {
e.printStackTrace();
}
}
}