Showing posts with label jsp. Show all posts

FileUpload in JSP  

Posted by Kishore in ,

Its been so long since i last blogged.
This time i am coming up with how to upload files in jsp.

The first and formost thing before doing this is to have the necessary framework to do this uploading.

You can use Apache Common FileUpload or com.oreilly.servlet.

Here i have used the second one.

In the jsp, the form should have the multipart encoding. To do this, please follow the following HTML snippet.

<form action="../AddCandidateServlet" method="post" enctype="multipart/form-data">

But if you do this, then the normail request.getParameter(str) will not be capable of reading the file or other input values from the particular form, so in order to do that specific task, we need to use the external frameworks.

The following example doPost method, reads all parameters and values. Also it stores the file in the specified location.

The following code is not my code, its been extracted from the site.

/*
* DemoParserUploadServlet.java
*
* Example servlet to handle file uploads using MultipartParser for
* decoding the incoming multipart/form-data stream
*/

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;

import com.oreilly.servlet.multipart.*;

public class DemoParserUploadServlet extends HttpServlet {
private File dir;

public void init(ServletConfig config) throws ServletException {
super.init(config);
// Read the uploadDir from the servlet parameters
String dirName; //= config.getInitParameter("uploadDir");
dirName="C:\\Program Files\\Apache Group\\Tomcat 4.1\\webapps\\examples\\WEB-INF\\classes\\cal";
//if (dirName == null) {
// throw new ServletException("Please supply uploadDir parameter");
//}
dir = new File(dirName);
if (! dir.isDirectory()) {
throw new ServletException("Supplied uploadDir " + dirName +
" is invalid");
}
}

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
PrintWriter out = response.getWriter();
response.setContentType("text/plain");
out.println("Demo Parser Upload Servlet");
if(false)
{

try {
MultipartParser mp = new MultipartParser(request, 1*1024*1024); // 10MB
Part part;
while ((part = mp.readNextPart()) != null) {
String name = part.getName();
if (part.isParam()) {
// it's a parameter part
ParamPart paramPart = (ParamPart) part;
String value = paramPart.getStringValue();
out.println("param; name=" + name + ", value=" + value);
}
else if (part.isFile()) {
// it's a file part
FilePart filePart = (FilePart) part;
String fileName = filePart.getFileName();
if (fileName != null) {
// the part actually contained a file
long size = filePart.writeTo(dir);
out.println("file; name=" + name + "; filename=" + fileName +
", filePath=" + filePart.getFilePath() +
", content type=" + filePart.getContentType() +
", size=" + size);

}
else {
// the field did not contain a file
out.println("file; name=" + name + "; EMPTY");
}
out.flush();
}
}
}
catch (IOException lEx) {
this.getServletContext().log(lEx, "error reading or saving file");
}
}

// If the next two lines are giving errors, you can comment it. But you will not be able to some other works with it.
CommandProcessor cp=new CommandProcessor();

cp.process(request,response);
HttpSession session=request.getSession();
String fname=(String)session.getAttribute("FILENAME");
out.println("returned from cp"+fname);
}
}

Unicode in jsp  

Posted by Kishore in ,

Using unicode in jsp or java files under influence of any IDE like RAD can be easier.

Before pasting your unicode data into the jsp,java or data file in RAD, make the file encoding into utf-8. You can do this by

  1. Select file in package explorer
  2. Right click and choose Properties
  3. Choose the Other option in the Text-Encoding.
  4. From the list choose UTF-8
Even though you specify the metadata content type tag as UTF-8, you still need to do the same thing using

<%@ page language="java" contentType="text/html; charset=UTF-8" %>

And here you go, your யூனிகோட்(Unicode) displays fine in the screen :)

Too many files open - jsp compilation error  

Posted by Kishore in , , ,

I was frequently getting too many files open compilation error in jsp in one of my portlet factory project.
OS: AIX
The server i used was Webphere Application Server 6.0.19, Websphere Portal Server 6.0.1.1

The problem was occuring in a totally weird manner.
When the first user accesses the application, it works completely fine.
The problem starts when the second user also logs in.
It gave "too many files open".

We tried hard to find a clue on this. Since this is totally abnormal behaviour of a web application server.

The mistake we did was not reading the stacktrace properly.
In the stack trace it clearly mentioned that it occured in Clone_Portal.

Bingo, we found atlast.
The problem is, when the application runs for the first time, all jsp files are loaded properly.
When the second user logs in, the server search for the files in the Clone portal, but the application is installed in the normal portal.

So we stopped the clone portal.
And it started workign fine.

Conclusion:
It is a problem in the clustered environment when the clone portal is running.