Showing posts with label Servlet. Show all posts
Showing posts with label Servlet. Show all posts

Tuesday, 4 December 2012

File Uploading through Servlet

Posted by Naveen Katiyar On 02:37 No comments
Steps to upload a file:-

1.Specify the request method POST.
2.Add the enctype attribute of form tag.ie.
<form action="test.htm" method="post" enctype="multipart/form-data"/>
3.use the <input type="file"/>
4.use the third party vendor specific class to upload the files at server side.
NOTE:cos.jar file contains a class MultipartRequest and this class is responsible to upload the files at server side.This class is defined in com.oreilly.servlet package.

If you dont want to use any third party jar then switch to servlet 3.0.

code of index.jsp:-

<html>
<body bgcolor="yellow">
<form action="test.htm" method="post" enctype="multipart/form-data">
enter name:<input type="text" name="textname"><br>
select file:<input type="file" name="file"><br>
<input type="submit" value="submit">
</form>
</body>
</html>

code of UploadServlet:-

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import com.oreilly.servlet.*;
public class UploadServlet extends HttpServlet
{
public void doPost(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException
{
try
{
response.setContentType("text/html");
PrintWriter out=response.getWriter();
out.println("<html><body bgcolor=yellow>");
String n=request.getParameter("textname");
out.println("userid:"+n);
String p=this.getServletConfig().getServletContext().getRealPath("");
p=p+"\\"+"upload";
MultipartRequest multi=new MultipartRequest(request,p,10*1024*1024);
String fn=multi.getOriginalFileName("file");
String id=multi.getParameter("textname");
out.println("userid:"+id);
out.println(fn+"file is uploaded");
out.println("</body></html>");
out.close();
}catch(Exception e){System.out.println(e);}
}
}

upload is the folder where file will be uploaded and is located parallel to WEB-INF.
*Note:-Request object is set to null after uploading.so,you should use multipart object to get the request parameters..

Click here to get working example

Downloading after clicking on a hyperlink

Posted by Naveen Katiyar On 02:36 No comments

Steps to develop download servlet:-


1.Set the header of HttpServlet Response i.e.
             response.setHeader("content-disposition","attachment;filename="+fname); where fname denotes the name of file to be downloaded on browser.

2.Set the ContentType of response i.e.
        response.setContentType("application/octet-stream");

3.Obtain the byte oriented output stream from the response i.e.
       OutputStream out=response.getOutputStream();

Servlet code is given as under:

 import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
public class DownloadServlet extends HttpServlet
{
public void service(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException
{
try
{
String name=request.getParameter("name");
response.setHeader("content-disposition","attachment;filename="+name);
response.setContentType("application/octet-stream");

OutputStream out=response.getOutputStream();
String downpath=this.getServletConfig().getServletContext().getRealPath("")+"\\"+"upload"+"\\"+name;
FileInputStream fin=new FileInputStream(downpath);
byte b[]=new byte[fin.available()];
fin.read(b);
out.write(b);
out.close();
fin.close();
}catch(Exception e){System.out.println(e);}
}
}

upload is name of a folder parallel to WEB-INF in which files to be downloaded are stored.


Click here to get working example

Sunday, 2 December 2012

AutoRefresh a webpage

Posted by Naveen Katiyar On 02:14 1 comment

  • AutoRefresh a webpage



To autorefresh a web page after a some time,you just need to set the response header with two parameters.In first parameter we specify refresh as string and in second parameter the seconds after which page is to be refreshed and url of web page where the page should redirect after refresh.



import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
public class AutoRefreshServlet extends HttpServlet
{
int i=0;
public void service(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException
{
try
{
response.setContentType("text/html");
response.setHeader("refresh","2;URL=http://www.facebook.com");
PrintWriter out=response.getWriter();
out.println("<html><body bgcolor=red>");
//out.println("this is auto refresh page:"+(++i));
out.println("<a href=http://www.google.com>google</a>");
out.println("<a href=http://www.facebook.com>facebook</a>");

out.println("</body></html>");
out.close();
}
catch(Exception e){System.out.println(e);}
}
}

  Click to get the source code of working example