public final class foo_jsp
{
// This is where the request comes in
public void _jspService(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException
{
// JspWriter instance is gotten from a factory
// This is why you can use 'out' directly in scriptlets
JspWriter out = ...;
// Snip
out.write("<html>");
out.write("<head/>");
out.write("<body>");
out.write(someOutput()); // i.e. write the results of the method call
out.write("</body>");
out.write("</html>");
}
// Directive gets translated as separate method - note
// there is no 'out' variable declared in scope
private String someOutput()
{
return "Some output";
}
}
It is my Computer Notes. Using Guide : object-key or object-key-key e.g. Step 1 Blog Search : [mysql-run] and then Step 2 ctrl+F [mysql-run] {bookMark me : Ctrl+D}
2014年11月26日 星期三
jsp2html
http://stackoverflow.com/questions/138999/how-to-output-html-from-jsp-block
2014年6月5日 星期四
java-variable java-global-variable , jsp-global-variable,jsp-variable
inf: http://www.easywayserver.com/blog/global-variable-in-jsp/
Global variable in JSP
Global variable in JSP
Global variable are variable which can access in whole JSP class in any method declared in class.
Global variable in JSP should be defined in declaration part <%! %>. Code inside this declaration can be accessed from any method e.g. jspInit(), jspDestroy(), and jspService().
jsp file
<%@ page language="java" %> <%! String sGlobalVariable="defining variable here and access anywhere in JSP"; public void getStringValue() { } %> <html> <head> <title>Global variable in JSP</title> </head> <body> <%=sGlobalVariable%> </body> </html>
When execute on web browser, it will create jsp to servlet and see the sGlobalVariable is not in service method of JSP, this means you can access this variable in any method in JSP
public final class global_jsp extends org.apache.jasper.runtime.HttpJspBase implements org.apache.jasper.runtime.JspSourceDependent { String sGlobalVariable="defining variable here and access anywhere in JSP"; public void getStringValue() { } public void _jspInit() { } public void _jspDestroy() { } public void _jspService(HttpServletRequest request, HttpServletResponse response) throws java.io.IOException, ServletException { } }
2014年2月12日 星期三
jsp-servlet
jsp call servlet
s="ViewPDF?db=data&viewFile="+s14+"&ContentType=application/vnd.ms-excel";
$('#moralFrame').prop('src',s);
2013年12月26日 星期四
java-upload file jsp-upload
http://www.jedi.be/blog/2009/04/10/java-servlets-and-large-large-file-uploads-enter-apache-fileupload/
http://www.avajava.com/tutorials/lessons/how-do-i-monitor-the-progress-of-a-file-upload-to-a-servlet.html?page=1 GOOD
http://davyjones2010.iteye.com/blog/1853422
htmlfile.html
<HTML>
<HEAD>
<TITLE>Display file upload form to the user</TITLE>
</HEAD>
<BODY>
<FORM ENCTYPE="multipart/form-data" ACTION="upload.jsp" METHOD=POST>
<br>
<br>
<br>
<center>
<table border="0" bgcolor=#ccFDDEE>
<tr>
<center>
<td colspan="2" align="center"><B>UPLOAD THE FILE</B>
<center>
</td>
</tr>
<tr>
<td colspan="2" align="center"></td>
</tr>
<tr>
<td><b>Choose the file To Upload:</b></td>
<td><INPUT NAME="file" TYPE="file"></td>
</tr>
<tr>
<td colspan="2" align="center"></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit"
value="Send File"></td>
</tr>
<table>
</center>
</FORM>
</BODY>
</HTML>
upload.jsp
<%@ page import="java.io.*"%>
<%
String saveFile = "";
String contentType = request.getContentType();
if ((contentType != null) && (contentType.indexOf("multipart/form-data") >= 0)) {
DataInputStream in = new DataInputStream(request.getInputStream());
int formDataLength = request.getContentLength();
byte dataBytes[] = new byte[formDataLength];
int byteRead = 0;
int totalBytesRead = 0;
while (totalBytesRead < formDataLength) {
byteRead = in.read(dataBytes, totalBytesRead, formDataLength);
totalBytesRead += byteRead;
}
String file = new String(dataBytes);
saveFile = file.substring(file.indexOf("filename=\"") + 10);
saveFile = saveFile.substring(0, saveFile.indexOf("\n"));
saveFile = saveFile.substring(saveFile.lastIndexOf("\\") + 1, saveFile.indexOf("\""));
int lastIndex = contentType.lastIndexOf("=");
String boundary = contentType.substring(lastIndex + 1, contentType.length());
int pos;
pos = file.indexOf("filename=\"");
pos = file.indexOf("\n", pos) + 1;
pos = file.indexOf("\n", pos) + 1;
pos = file.indexOf("\n", pos) + 1;
int boundaryLocation = file.indexOf(boundary, pos) - 4;
int startPos = ((file.substring(0, pos)).getBytes()).length;
int endPos = ((file.substring(0, boundaryLocation)).getBytes()).length;
//saveFile = "C:/himanshu/" + saveFile;
saveFile = "/erp/" + saveFile;
File ff = new File(saveFile);
FileOutputStream fileOut = new FileOutputStream(ff);
out.println(startPos);
out.println(endPos);
out.println(dataBytes);
//String content = "This is the text content";
//byte[] contentInBytes = content.getBytes();
//fileOut.write(contentInBytes, startPos, (endPos - startPos));
fileOut.write(dataBytes, startPos, (endPos - startPos));
int i=endPos-startPos;
//fileOut.write(dataBytes,startPos,i);
fileOut.flush();
fileOut.close();
%><Br>
<table border="2">
<tr>
<td><b>You have successfully upload the file by the name of:</b>
<%
out.println(saveFile);
}
%>
</td>
</tr>
</table>
=================================================
Processing the items is easy. And check if it a file part
The streaming API:
As described in http://commons.apache.org/fileupload/streaming.html fileupload provides a way to avoid the write to disk before your servlet can handle.
We're allmost there. Now that we have the name, the stream we can write it to the correct place on this, without tempfiles and overloaded memory!
http://java.sun.com/docs/books/performance/1st_edition/html/JPIOPerformance.fm.html describes different ways you can use to write your file to disk
Option 1 : The naive way, we take the inputstream and write it byte per byte to an outputstream.
Again experiment with the buffersize
We optimized our file writing. But we can also improve our network handling.
You can look at grizzly, glassfish, or jetty to use the NIO capabilities of Java.
http://www.avajava.com/tutorials/lessons/how-do-i-monitor-the-progress-of-a-file-upload-to-a-servlet.html?page=1 GOOD
http://davyjones2010.iteye.com/blog/1853422
htmlfile.html
<HTML>
<HEAD>
<TITLE>Display file upload form to the user</TITLE>
</HEAD>
<BODY>
<FORM ENCTYPE="multipart/form-data" ACTION="upload.jsp" METHOD=POST>
<br>
<br>
<br>
<center>
<table border="0" bgcolor=#ccFDDEE>
<tr>
<center>
<td colspan="2" align="center"><B>UPLOAD THE FILE</B>
<center>
</td>
</tr>
<tr>
<td colspan="2" align="center"></td>
</tr>
<tr>
<td><b>Choose the file To Upload:</b></td>
<td><INPUT NAME="file" TYPE="file"></td>
</tr>
<tr>
<td colspan="2" align="center"></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit"
value="Send File"></td>
</tr>
<table>
</center>
</FORM>
</BODY>
</HTML>
upload.jsp
<%@ page import="java.io.*"%>
<%
String saveFile = "";
String contentType = request.getContentType();
if ((contentType != null) && (contentType.indexOf("multipart/form-data") >= 0)) {
DataInputStream in = new DataInputStream(request.getInputStream());
int formDataLength = request.getContentLength();
byte dataBytes[] = new byte[formDataLength];
int byteRead = 0;
int totalBytesRead = 0;
while (totalBytesRead < formDataLength) {
byteRead = in.read(dataBytes, totalBytesRead, formDataLength);
totalBytesRead += byteRead;
}
String file = new String(dataBytes);
saveFile = file.substring(file.indexOf("filename=\"") + 10);
saveFile = saveFile.substring(0, saveFile.indexOf("\n"));
saveFile = saveFile.substring(saveFile.lastIndexOf("\\") + 1, saveFile.indexOf("\""));
int lastIndex = contentType.lastIndexOf("=");
String boundary = contentType.substring(lastIndex + 1, contentType.length());
int pos;
pos = file.indexOf("filename=\"");
pos = file.indexOf("\n", pos) + 1;
pos = file.indexOf("\n", pos) + 1;
pos = file.indexOf("\n", pos) + 1;
int boundaryLocation = file.indexOf(boundary, pos) - 4;
int startPos = ((file.substring(0, pos)).getBytes()).length;
int endPos = ((file.substring(0, boundaryLocation)).getBytes()).length;
//saveFile = "C:/himanshu/" + saveFile;
saveFile = "/erp/" + saveFile;
File ff = new File(saveFile);
FileOutputStream fileOut = new FileOutputStream(ff);
out.println(startPos);
out.println(endPos);
out.println(dataBytes);
//String content = "This is the text content";
//byte[] contentInBytes = content.getBytes();
//fileOut.write(contentInBytes, startPos, (endPos - startPos));
fileOut.write(dataBytes, startPos, (endPos - startPos));
int i=endPos-startPos;
//fileOut.write(dataBytes,startPos,i);
fileOut.flush();
fileOut.close();
%><Br>
<table border="2">
<tr>
<td><b>You have successfully upload the file by the name of:</b>
<%
out.println(saveFile);
}
%>
</td>
</tr>
</table>
=================================================
InputStream in= From the FileUpload API;int bytesRead=0;int bytesToRead=4096;byte[] input = new byte[bytesToRead];ByteArrayOutputStream baos = null;while (bytesRead < bytesToRead) { int result = in.read(input, bytesRead, bytesToRead - bytesRead); if (result == -1) break; bytesRead += result; baos=new ByteArrayOutputStream(); baos.write(input,0,input.length); content.appendContent(baos);//Content Management API baos.close();
======================================================================
Just Enough Developed Infrastructure
Java Servlets and Large, Large file Uploads: enter apache fileupload
Having examined the rails alternatives for large file upload, we turned towards other alternatives and YES , did we find one!
It's the java fileupload component from the apache commons project. It has the following features:
It's the java fileupload component from the apache commons project. It has the following features:
- you can control the memory size within your servlet
- you have direct access to the incoming stream without any temporary file
- you have a streaming api for processing the different multipart streams
- the non-streaming API
- the streaming version
- the streaming version + combinations of writing FileIO using various buffers.
The basic example:( non streaming)
As described in http://commons.apache.org/fileupload/using.html You can specify <yourMaxMemorySize>, <yourTempDirectory>, <yourMaxRequestSize>
// Create a factory for disk-based file items
DiskFileItemFactory factory = new DiskFileItemFactory();
// Set factory constraints
factory.setSizeThreshold(yourMaxMemorySize);
factory.setRepository(yourTempDirectory);
// Create a new file upload handler ServletFileUpload upload = new ServletFileUpload(factory); // Set overall request size constraint upload.setSizeMax(yourMaxRequestSize); // Parse the request List / FileItem / items = upload.parseRequest(request);
Processing the items is easy. And check if it a file part
// Process the uploaded items
Iterator iter = items.iterator();
while (iter.hasNext()) {
FileItem item = (FileItem) iter.next();
if (item.isFormField()) {
processFormField(item);
} else {
processUploadedFile(item);
}
}
You access the file characteristics like this:// Process a file upload
if (!item.isFormField()) {
String fieldName = item.getFieldName();
String fileName = item.getName();
String contentType = item.getContentType();
boolean isInMemory = item.isInMemory();
long sizeInBytes = item.getSize();
...
}
default implementation of FileUpload, write() will attempt to rename the file to the specified destination, but if you want you can read the stream directly.// Process a file upload
if (writeToFile) {
File uploadedFile = new File(...);
item.write(uploadedFile);
} else {
InputStream uploadedStream = item.getInputStream();
...
uploadedStream.close();
}
As described in http://commons.apache.org/fileupload/streaming.html fileupload provides a way to avoid the write to disk before your servlet can handle.
// Check that we have a file upload request boolean isMultipart = ServletFileUpload.isMultipartContent(request);Now we are ready to parse the request into its constituent items. Here's how we do it:
// Create a new file upload handler
ServletFileUpload upload = new ServletFileUpload();
// Parse the request
FileItemIterator iter = upload.getItemIterator(request);
while (iter.hasNext()) {
FileItemStream item = iter.next();
String name = item.getFieldName();
InputStream stream = item.openStream();
if (item.isFormField()) {
System.out.println("Form field " + name + " with value " + Streams.asString(stream) + " detected.");
} else {
System.out.println("File field " + name + " with file name " + item.getName() + " detected.");
// Process the input stream
...
}
}
How to write is to disk most efficiently:We're allmost there. Now that we have the name, the stream we can write it to the correct place on this, without tempfiles and overloaded memory!
http://java.sun.com/docs/books/performance/1st_edition/html/JPIOPerformance.fm.html describes different ways you can use to write your file to disk
Option 1 : The naive way, we take the inputstream and write it byte per byte to an outputstream.
FileOutputStream fout= new FileOutputStream (yourPathtowriteto);
int byte;
while ((byte=stream.read()) != -1)
{
fout.write(byte_);
}
fout.close();
Option 2: We use bufferstreams to do the jobFileOutputStream fout= new FileOutputStream ( yourPathtowriteto );
BufferedOutputStream bout= new BufferedOutputStream (fout);
BufferedInputStream bin= new BufferedInputStream(stream);
int byte;
while ((byte=bin.read()) != -1)
{
bout.write(byte_);
}
bout.close();
bin.close();
Option 3: Use a byte array instead of per byte. You can experiment with different buffersize to see the effect. This depends on your filesystem blocksize and your disk cache size and so on. FileOutputStream fout= new FileOutputStream (yourPathtowriteto);
BufferedOutputStream bout= new BufferedOutputStream (fout);
BufferedInputStream bin= new BufferedInputStream(stream);
byte buf[] = new byte[2048];
while ((bin.read(buf)) != -1)
{
bout.write(buf);
}
bout.close();
bin.close();
Option 4: Using a static byte array: to avoid reallocation , we create a final buffer and use synchronized to control access to it.Again experiment with the buffersize
static final int BUFF_SIZE = 100000;
static final byte[] buffer = new byte[BUFF_SIZE];
FileOutputStream fout= new FileOutputStream (yourPathtowriteto);
while (true) {
synchronized (buffer) {
int amountRead = stream.read(buffer);
if (amountRead == -1) {
break;
}
fout.write(buffer, 0, amountRead);
}
}
Controlling network I/O in your appserverWe optimized our file writing. But we can also improve our network handling.
You can look at grizzly, glassfish, or jetty to use the NIO capabilities of Java.
}
訂閱:
文章 (Atom)