Skip to main content

Posts

Uploading file using Servlet/JSP with the help of apache commons-fileupload library

Here is the example to upload the file using commons fileupload library. package  imagesvideos.webcontroller; import  java.io. File ; import  java.io. IOException ; import  java.io. PrintWriter ; import  java.util. List ; import  javax.servlet.ServletException; import  javax.servlet.http.HttpServlet; import  javax.servlet.http.HttpServletRequest; import  javax.servlet.http.HttpServletResponse; import  org.apache.commons.fileupload.FileItem; import  org.apache.commons.fileupload.FileUploadException; import  org.apache.commons.fileupload.disk.DiskFileItemFactory; import  org.apache.commons.fileupload.servlet.ServletFileUpload; public   class  UploadImageVideo  extends  HttpServlet  {      private   static   final   long  serialVersionUID  =   1L ;      protected   void   doPost ( HttpServletRequest...

Spring MVC - Populating domain object (Form backing Object) in view and updating some properties

Sometimes we need to update the domain object partially. We get complete object from database but we have to update some fields of it using html form. In this situation, original Data Object is kept on the server and some fields are asked to be filled by user. In Sprint MVC, we have implemented feature that helps us to get it done. There is @ModelAttribute annotation that captures Bean object at server and maps its values to submitted form's fields. In following code, pupulateContact() method annotated with @ModelAttribute annotation. This make sure that this method is called before any @RequestMapping annotated method like open() and save() in this code. pupulateContact() method returns an object having some fields initialized. We want to get other fields from HTML form. After submission ,Contact Object will have all values in it. It remembers the initialized value and get updated with new values those are sent in request. Other will remain as it is. package  pack; ...

Understanding run time polymorphism of an object

Run-time polymorphism is the ability of an object to play multiple roles at run-time. See the following program where the object of Cal class will have different behaviors according the reference variable through which it is pointed. If we access Cal object using c, it has add() and sub() methods. If we access it using a reference variable then only add() method is accessible and same is for b variable that allows to access only sub() method. So in spite of having same object, It is playing different role according to the type of reference variable that is pointing it. //Example of polymorphism package  poly; interface  Adder {      void   add ( int  x, int  y ) ; } interface  Substractor {      void   sub ( int  x, int  y ) ; } class  Cal  implements  Adder,Substractor {     @Override      public   void   add ( in...

JEE : Java Server Page (JSP) life cycle

All the JSP run under supervising of web server. They do not receive requests from client and they do not send the response to client. All the JSP pages pass through the Server side processing. And result of sever side processing is sent to client. That is why they called dynamic pages. In the case of static page there is no server processing, web server returns them as they are. Dotted line showing that, When client sends the request for static page , server returns the requested page without processing and solid line showing that when client sends the request for dynamic page, server process it and the response generated is returned to client. How the JSP executes 1. A request for a JSP pages is made by the client. 2. The request is handled by the web server. 3. Then the request is delegated to the JSP container 4. Container will check whether JSP to be invoked is changed or not 5. If there are some changes or its new JSP page then engine translates the contents of ...

Popular Logics for implementation to check basic skills of fresher

Factorial long Factorial( int n ) { if ( n>0 ) return( n * Factorial(n-1) ); else return( 1 ); } Fibonacci int Fibonacci( int n ) { if ( n==1 || n==2 ) return( 1 ); else return( Fibonacci(n-1) + Fibonacci(n-2) ); } GCD int GCD( int a, int b ) { if ( a>=b && a%b==0 ) return( b ); else if ( a<b ) return( GCD( b, a ) ); else return( GCD( b, a%b ) ); } Power double Power( double x, int n ) { if ( n==0 ) return( 1 ); else if ( n>0 ) return( x * Power( x, n-1 ) ); else return( (1/x) * Power( x, n+1 ) ); } Reverse Printing void ReverseChar( void ) { char ch; if ( (ch=getchar( ))!='n' ) ReverseChar( ); putchar( ch ); } Decimal to binary conversion void ToBin( int n ) /* { if (n>1) ToBin( n/2 ); printf( "%d", n%2 ); } Decimal to hexadecimal conversion void ToHex( int n ) { char *htab[ ] = { "0", "1", "2", "3", "4", "5", "6", "7...

JSP SCRIPTING ELEMENTS

Expression tag ( <%= %>) This tag is used to sent the output of the expression to the output stream like method println(). This tag allows the developer to embed any Java expression and is short for out.println(). This tag starts with <%= and ends with %>. A semicolon ( ; ) does not appear at the end of the code inside the tag. For example,to show the current date and time. <HTML> <HEAD> <TITLE>MCA</TITLE> </HEAD> <BODY> Date : <%= new java.util.Date() %> </BODY> </HTML> Declaration tag ( <%! %> ) This tag allows the developer to declare variables or methods. Before the declaration you must have <%! At the end of the declaration, the developer must have %> Code placed in this tag must end in a semicolon ( ; ). Declarations do not generate output so are used with JSP expressions or scriptlets. For Example, <%! S...

JSP : Predifined variables | implicit objects

There are following implicit objects those become available to programmer to use in JSP page.   out The out implicit object is an instance of a javax.servlet.jsp.JspWriter object and is used to send content in a response. The JspWriter object emulates some of the functionality found in the java.io.PrintWriter and java.io.BufferedWriter objects to provide a convenient method of writing text in a buffered fashion. The out implicit object can be configured on a per JSP basis by the page directive. Example : <HTML> <HEAD> <TITLE>JSP Demo</TITLE> </HEAD> <BODY> <% out.write("Server Date is: "); %> <% out.write(new java.util.Date().toString()); %> </BODY> </HTML>   request The request implicit object is an instance of a javax.servlet.http.HttpServletRequest object. The request implicit object represents a client's request and is a reference to the HttpServletRequest object passed ...