Saturday, 2 December 2017

Hoe to upload a file on website unsig java jsp programming

Learning Java Programming Made Easy


HTML/JSP View File:

<%-- 
    Document   : upload
    Created on : Feb 14, 2017, 12:25:46 PM
    Author     : my pc
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<body>
Select a file to upload: <br />
<form action="uploadtarget.jsp" method="post" enctype="multipart/form-data">
<input type="file" name="file" size="50" />
<br />
<input type="submit" value="Upload File" />
</form>
</body>
</html>


/**********************************************************/

JSP Controller File:     uploadtarget.jsp

<%-- 
    Document   : uploadtarget'
    Created on : Feb 14, 2017, 12:26:40 PM
    Author     : my pc
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<%@page import="java.io.*,java.util.*,javax.servlet.*" %>
<%@ page import="javax.servlet.http.*" %>
<%@ page import="org.apache.commons.fileupload.*" %>
<%@ page import="org.apache.commons.fileupload.disk.*" %>
<%@ page import="org.apache.commons.fileupload.servlet.*" %>
<%@ page import="org.apache.commons.io.output.*" %>
<%
   File file ;
   int maxFileSize = 5000 * 1024;
   int maxMemSize = 5000 * 1024;
   String filePath = "c:/apache-tomcat/webapps/data/";

   String contentType = request.getContentType();
   if ((contentType.indexOf("multipart/form-data") >= 0)) {

      DiskFileItemFactory factory = new DiskFileItemFactory();
      factory.setSizeThreshold(maxMemSize);
      factory.setRepository(new File("c:\\temp"));
      ServletFileUpload upload = new ServletFileUpload(factory);
      upload.setSizeMax( maxFileSize );
      try{ 
         List fileItems = upload.parseRequest(request);
         Iterator i = fileItems.iterator();
         out.println("<html>");
         out.println("<body>");
         while ( i.hasNext () ) 
         {
            FileItem fi = (FileItem)i.next();
            if ( !fi.isFormField () )  {
                String fieldName = fi.getFieldName();
                String fileName = fi.getName();
                boolean isInMemory = fi.isInMemory();
                long sizeInBytes = fi.getSize();
                file = new File( filePath + "yourFileName") ;
                fi.write( file ) ;
                out.println("Uploaded Filename: " + filePath + fileName + "<br>");
            }
         }
         out.println("</body>");
         out.println("</html>");
      }catch(Exception ex) {
         System.out.println(ex);
      }
   }else{
      out.println("<html>");
      out.println("<body>");
      out.println("<p>No file uploaded</p>"); 
      out.println("</body>");
      out.println("</html>");
   }
%>




How to use auto refresh using java jsp program code

Learning Java Programming Made Easy




<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<%@ page import="java.io.*,java.util.*;" %>
<html>
<body>
<%
   response.setIntHeader("Refresh", 1);
   Calendar calendar = new GregorianCalendar();

   String am_pm;
   int hour = calendar.get(Calendar.HOUR);
   int minute = calendar.get(Calendar.MINUTE);
   int second = calendar.get(Calendar.SECOND);
   if(calendar.get(Calendar.AM_PM) == 0)
      am_pm = "AM";
   else
      am_pm = "PM";
   String CT = hour+":"+ minute +":"+ second +" "+ am_pm;
   out.println( CT + "\n");
%>
</body>
</html>

How to use if loop in java programming

Learning Java Programming Made Easy



import java.io.*
import java.util.*

public class ifloop
{
public static void main(String args[])
{
int x;
Scanner sc=new Scanner(System.in);
System.out.println("Enter 1 or 2");
x=sc.nextInt();
if(x==1)
{
System.out.println("Well Done...! You Entered x="+x);
}
elseif(x==2)
{
System.out.println("Congrats...! You Entered x="+x);
}
else
{
System.out.println("Sorry...! Invalid Numbar");
}
}
}

How to use for loop in java program

Learning Java Programming Made Easy


import java.io.*;
import java.util.*;

public class forloop
{
public static void main(String args[])
{
int x,y;
Scanner sc=new Scanner(System.in);
System.out.println("Enter the maximum count");
y=sc.nextInt();
for(x=0;x<y;x++)
{
System.out.println("x="+x);
}
}
}

How to use Do-While loop in java

Learning Java Programming Made Easy

import java.io.*;
import java.util.*;

public class do_while
{
public static void main(String args[])
{
int x=0;
do
{
x=x+1;
System.out.println("x="+x);
}
while(x!=10);
}
}

Tuesday, 28 February 2017

How to use java code for type casting

//********************************Start**************************

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package javaapplication1;

/**
 *
 * @author HP
 */
public class Java_Type_Casting {
 public static void main(String args[])
    {

        byte b;
        int i = 257;
        double d = 323.142;

        System.out.println("Conversion of int to byte.");
        b = (byte) i;
        System.out.println("i is " + i + " and b is " + b);

        System.out.println("\nConversion of double to int.");
        i = (int) d;
        System.out.println("d is " + d + " and i is " + i);

        System.out.println("\nConversion of double to byte.");
        b = (byte) d;
        System.out.println("d is " + d + " and b is " + b);

    }
}

//********************************End***************************

How to use Java Code for Login Form design

//*****************************Start************************

import java.awt.*;
import javax.swing.*;
/**
 *
 * @author HP
 */
public class AWT_Login_From {
    JFrame f;
    JPanel jp;
    JLabel heading,username,password,forgetpass;
    JTextField tuser;
    JPasswordField pass;
    JButton login,reset;

    public AWT_Login_From()
    {
        f=new JFrame("Login Form");
        f.setSize(400,300);
        f.setVisible(true);
        f.setLocation(500, 250);
        f.setResizable(false);
        jp=new JPanel();
        jp.setLayout(null);
        f.add(jp);


        heading=new JLabel("Login Form");
        heading.setBounds(150, 20, 150, 50);
        jp.add(heading);

        username=new JLabel("USER NAME");
        username.setBounds(60, 100, 80, 10);
        jp.add(username);

        password=new JLabel("PASSWORD");
        password.setBounds(60, 150, 80, 10);
        jp.add(password);

        forgetpass=new JLabel("Forget Password");
        forgetpass.setBounds(210, 170, 100, 10);
        jp.add(forgetpass);

        tuser=new JTextField("");
        tuser.setBounds(140,95,170,25);
        jp.add(tuser);

        pass=new JPasswordField();
        pass.setBounds(140,145,170,25);
        jp.add(pass);

        login=new JButton("Login");
        login.setBounds(60,205,100,25);
        jp.add(login);

        reset=new JButton("Reset");
        reset.setBounds(240,205,100,25);
        jp.add(reset);

     
    }
public static void main(String args[])
    {
    AWT_Login_From lf=new AWT_Login_From();


}
}


//*****************************End*************************

How to use Java Code to Print 1 to 45 In Triangle Shape...

//*************************Start**************************

public class Print_1_TO_45_In_Triangle
{
    public static void main(String args[])
    {
        int n=1, i, j;
        for(i=0; i<10; i++)
        {
            for(j=0; j<i; j++)
            {
                if(0<n&&n<10)
                {
                   System.out.print("0");
                }

                System.out.print(n + "  ");
                n++;
            }
            System.out.println();
        }
    }
}


//*************************ENd**************************

How to use Java for ShutDown Computer From using Code

//******************Start**************************

import java.io.*;

/**
 *
 * @author HP
 */
public class Shut_Down_Computer {
public static void main(String args[]) throws IOException
    {
        Runtime runtime = Runtime.getRuntime();
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

        System.out.print("Enter No. of Seconds after which You want your Computer to Shutdown :");
        long a=Long.parseLong(br.readLine());

        Process proc = runtime.exec("shutdown -s -t " +a);

        System.exit(0);
    }
}


//******************End***************************

How to use Java Code Example for Try-Catch Function to Get Exception Error

//**************************Start***************************

import java.util.*;

/**
 *
 * @author HP
 */
public class try_catch {

    public static void main(String args[]) {
        int i, j;
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter any 2 no.");
        i = sc.nextInt();
        j = sc.nextInt();
        try {
            int result = i / j;
            System.out.println("Result= " + result);
        }

        catch (Exception es) {
            System.err.println(es.getMessage());
        }
    }
}


//**************************End***************************

Charactor_At_Position

public class Char_At_Position { public static void main(String[] args) { String str = "Wankhede"; String rev=""...