JSP built-in Objects (iii): Application

Source: Internet
Author: User

One: Get file path

<%@ 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" >


<title>my JSP ' getpath.jsp ' starting page</title>

<body>
<%
String Path=application.getrealpath ("/");
%>
</body>


Two: Get the input content

<% @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" >


<title>my JSP ' input_content.jsp ' starting page</title>

<body>
<form action= "input_content.jsp" method= "POST" >
Input file name: <input type= "text" name= "filename" ><br>
Input file contents: <textarea name= "Filecontent" rows= "2" cols= "></textarea><br>"
<input type= "Submit" value= "Submission" >
<input type= "reset" value= "reset" >
</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);
Determine if a parent folder exists
if (!file.getparentfile (). exists ()) {
File.getparentfile (). mkdir ();
}
PrintStream Ps=null;
Ps=new PrintStream (new FileOutputStream (file));
PS.PRINTLN (content);
Ps.close ();
%>
<%
Read content
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>


Three: Website counter

<% @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" >


<title>my JSP ' count.jsp ' starting page</title>


<body>
<%!//Defining a global variable
BigInteger Count=null;
%>
<%!
Reading a Count file
Public BigInteger Load (file file) {
Receive the Read data
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{
First time visit
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);
}
}
%>
</body>


Four: viewing attribute ranges

<%@ 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" >


<title>my JSP ' application.jsp ' starting page</title>

<body>
<%
Enumeration Enu=this.getservletcontext (). Getattributenames ();
while (Enu.hasmoreelements ()) {
String Name= (String) enu.nextelement ();
%>
<%
}
%>
</body>

Copyright Notice: Bo Master original articles, reproduced please indicate the source. Http://blog.csdn.net/dzy21

JSP built-in Objects (iii): Application

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.