标签:jsp
一:获取文件路径
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP ‘getPath.jsp‘ starting page</title>
</head>
<body>
<%
String path=application.getRealPath("/");
%>
<h3>真实路径:<%=path %></h3>
</body>
</html>
二:获取输入的内容
<%@page import="java.io.*"%>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP ‘input_content.jsp‘ starting page</title>
</head>
<body>
<form action="input_content.jsp" method="post">
输入文件名称:<input type="text" name="filename"><br>
输入文件内容:<textarea name="filecontent" rows="2" cols="30"></textarea><br>
<input type="submit" value="提交">
<input type="reset" value="重置">
</form>
<%
request.setCharacterEncoding("utf-8");
String name=request.getParameter("filename");
String content=request.getParameter("filecontent");
String fileName=this.getServletContext().getRealPath("/")
+"note"+File.separator+name;
File file=new File(fileName);
//判断是否存在父文件夹
if(!file.getParentFile().exists()){
file.getParentFile().mkdir();
}
PrintStream ps=null;
ps=new PrintStream(new FileOutputStream(file));
ps.println(content);
ps.close();
%>
<%
//读取内容
Scanner scan=new Scanner(new FileInputStream(file));
scan.useDelimiter("\n");
StringBuffer buf=new StringBuffer();
while(scan.hasNext()){
buf.append(scan.next()).append("<br>");
}
scan.close();
%>
<%=buf %>
</body>
</html>
三:网站计数器
<%@page import="java.io.FileOutputStream"%>
<%@page import="java.io.PrintStream"%>
<%@page import="java.io.FileInputStream"%>
<%@page import="javax.servlet.jsp.tagext.TryCatchFinally"%>
<%@page import="java.io.File"%>
<%@page import="java.math.BigInteger"%>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP ‘count.jsp‘ starting page</title>
</head>
<body>
<%!//定义一个全局变量
BigInteger count=null;
%>
<%!
//读取计数文件
public BigInteger load(File file){
//接收读取的数据
BigInteger count=null;
try{
if(file.exists()){
Scanner scan=null;
scan=new Scanner(new FileInputStream(file));
if(scan.hasNext()){
count=new BigInteger(scan.next());
}
scan.close();
}else{
//第一次访问
count=new BigInteger("0");
save(file,count);
}
}catch(Exception e){
e.printStackTrace();
}
return count;
}
public void save(File file,BigInteger count){
try{
PrintStream ps=null;
ps=new PrintStream(new FileOutputStream(file));
ps.println(count);
ps.close();
}catch(Exception e){
e.printStackTrace();
}
}
%>
<%
String filename=this.getServletContext().getRealPath("/")+"count.txt";
File file=new File(filename);
if(session.isNew()){
synchronized(this){
count=load(file);
count=count.add(new BigInteger("1"));
save(file,count);
}
}
%>
<h3>第<%=count==null?0:count %>位访客!</h3>
</body>
</html>
四:查看属性范围
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP ‘application.jsp‘ starting page</title>
</head>
<body>
<%
Enumeration enu=this.getServletContext().getAttributeNames();
while(enu.hasMoreElements()){
String name=(String)enu.nextElement();
%>
<h4><%=name %>--><%=this.getServletContext().getAttribute(name) %></h4>
<%
}
%>
</body>
</html>
版权声明:博主原创文章,转载请说明出处。http://blog.csdn.net/dzy21
标签:jsp
原文地址:http://blog.csdn.net/dzy21/article/details/47188205