Saturday, 2 December 2017

How to send data from one page to other page java jsp programming code

Learning Java Programming Made Easy


Page 1: first page (html/jsp)



<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <!--script let java code-->
        <%
            out.println("<h1>hello how are u</h1>");
        %>
        <br>
        <!--expression java code-->
        <%= "hi hello how are u"%>
        <br>
        <!--declaration java code-->
        <%! int age = 18;%>
        <%!

            int sum(int i, int j) {
                return i + j;
            }

        %>  
        <%= "the sum of 2 no=" + sum(4, 5)%><br>
        <%= "the age of emp=" + age%><br>
        <jsp:forward page="forwrod.jsp"></jsp:forward>
    </body>
</html>



Page 2: Second page (HTMl/JSP) 

<%-- 
    Document   : second
    Created on : Jan 31, 2017, 11:33:55 AM
    Author     : my pc
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>

        <form action="third.jsp">
            username:<input type="text" name="user"/>
            <input type="submit" value="click"/>
        </form>
           
    </body>
</html>




Page 3: Third page (html/jsp)



<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
      <%
      String str=request.getParameter("user");
      out.println("username="+str);
      %>
    </body>
</html>

How to get todays date using java jsp code and programming

Learning Java Programming Made Easy



<%-- 
    Document   : date
    Created on : Feb 14, 2017, 12:12:49 PM
    Author     : my pc
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html> 
<body>
<p>
   Today's date: <%= (new java.util.Date().toString())%>
</p>
</body> 
</html>

How to call stored COOKIES using java jsp programming code and get data out

Learning Java Programming Made Easy




<%@page contentType="text/html" pageEncoding="UTF-8"%>

<!DOCTYPE html>

<html>

<body>

    <form action="coock.jsp" method="GET">

First Name: <input type="text" name="first_name">

<br />
Last Name: <input type="text" name="last_name" />
<input type="submit" value="Submit" />
</form>

</body>



How to store COOKEIS using java jsp code

Learning Java Programming Made Easy



<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<%
   // Create cookies for first and last names.      
   Cookie firstName = new Cookie("first_name",request.getParameter("first_name"));
   Cookie lastName = new Cookie("last_name",request.getParameter("last_name"));
   // Set expiry date after one hour for both the cookies.
   firstName.setMaxAge(1*1); 
   lastName.setMaxAge(1*1); 
   // Add both the cookies in the response header.
   response.addCookie( firstName );
   response.addCookie( lastName );
%>
<html>
<body>
<b>First Name:</b><%= request.getParameter("first_name")%><br/>
<b>Last  Name:</b><%= request.getParameter("last_name")%>
</body>
</html>

How to get IP using Java jsp programming

Learning Java Programming Made Easy




<%-- 
    Document   : ip
    Created on : Feb 14, 2017, 12:11:42 PM
    Author     : my pc
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head><title>Hello World</title></head>
<body>
Hello World!<br/>
<%
out.println("Your IP address is " + request.getRemoteAddr());
%>
</body>
</html>

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);
}
}

Charactor_At_Position

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