Saturday, June 30, 2012

resize image to required height and width

 How to resize image to required height and width. 


To resize the image firstly need to create a method  'resizeImage()' and pass the required parameters .

Here the 'resizeImage()' method accept 3 parameters (input Stream, required Width , required height )
the code is given below.

public static BufferedImage resizeImage(InputStream in, int requiredWidth, int requiredHeight) throws IOException {
        BufferedImage bfrImage = ImageIO.read(in);


        int imageWidth = bfrImage.getWidth();
        int imageheight = bfrImage.getHeight();
        double scalex = (double) requiredWidth / imageWidth;
        double scaley = (double) requiredHeight / imageheight;
        Image scaledImage = null;

 scaledImage = bfrImage.getScaledInstance(imageWidth, imageheight, Image.SCALE_SMOOTH);

BufferedImage destination = new BufferedImage(requiredWidth, requiredHeight, BufferedImage.SCALE_SMOOTH);
        Graphics2D g2d = destination.createGraphics();
        g2d.scale(scalex, scaley);
        g2d.drawImage(scaledImage, 0, 0, null);
        g2d.dispose();


        return destination;
    }


Thursday, June 28, 2012

access files inside web inf folder



  how to access files inside web inf folder



Direct accessing to web-inf is not possible by client. And its
restricted. Files inside the WEB-INF is only accessible by the
server.To access files inside web-inf we need to create a
java servlet.

using that servlet we can redirect the request to specified file
inside the web-inf folder. Here we are using a sample pure jsp
project that uses a login page 'login.jsp'. And its inside the
WEB-INF folder. Inside WEB-INF/jsp/login.jsp.

This example code will show how toredirect the request to 
login.jsp inside the web-inf folder.To do this firstly we need 
to modify the web.xml.


After modifying the web.xml create a class names
LoginController.java'and place the class inside'servletmapping'
package. Code is given below


pckage servletmapping;

import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class LoginController extends HttpServlet {


@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException{
String location = "/WEB-INF/jsp/login.jsp";
// location of jsp file inside the web inf


RequestDispatcher rd = getServletContext().getRequestDispatcher(location);
rd.forward(req, resp);
}
}


This is how we redirect the request inside the WEB-INF folder.
when we request '/app/login.jsp' then the corresponding
servlet handle the request(that we are defined inside the
web.xml) and serve the file inside the web inf.And it is not
possible to access the file by typing the exact
location inside the server that is 'WEB-INF/jsp/login.jsp'