Showing posts with label jsp. Show all posts
Showing posts with label jsp. Show all posts

Friday 5 July 2013

JSP Elements

// siddhu vydyabhushana // 1 comment
In previous example described in introduction to JSP, We didn`t use any java statement or code in the file. If the developer wants to put any java code in that file then the developer can put it by the use of jsp tags(elements).
There are mainly three group of jsp tags(elements) available in java server page :
1 ) JSP Scripting elements
2 ) JSP Directive elements
3 ) JSP Standard Action elements
All three elements described in next three topic.

Read More

JSP Scripting Elements

// siddhu vydyabhushana // 1 comment
There are four types of tag in jsp scripting elements.
a) JSP Declaration tag.
b) JSP scriptlet tag.
c) JSP Expression tag.
d) Comment tag.
Now we describe each tag in detail.

a) JSP Declaration tag :

A JSP declaration lets you declare or define variables and methods (fields) that use into the jsp page. 
Declaration tag Start with  <%! And End with %> 
The Code placed inside this tag must end with a semicolon (;).
Syntax:    <%! Java Code; %>
EX.
 <%! private int i = 10; %>
       <%! private int squre(int i) 
       {
              i = i * i ;
              return i;
       }	
 %> 

You can also put these codes in a single syntax block of declaration like,
 <%! private int i = 10;
        private int squre(int i)
	{
         	i = i * i ;
        	return i;
        }	
%> 

Note :  The XML authors can use an alternative syntax for JSP declaration tag: 
<jsp:declaration> 
      Java code;
</jsp:declaration>
EX.
 <jsp:declaration> private int i = 1; </jsp:declaration>
  <jsp:declaration>
    {
	private int squre(int i)
	i = i * i ;
	return i;
    }	
</jsp:declaration> 


You can also put these codes in a single syntax block of declaration like,
 <jsp:declaration> 
      private int i = 1; 
	private int squre(int i)
	{
		i = i * i ;
	      return i;
	}	
</jsp:declaration> 

Remember that XML elements, unlike HTML ones, are case sensitive. So be sure to use lowercase.
Declarations do not generate any output so the developer uses declaration tag with JSP expression tag or JSP scriptlet tag for generating an appropriate output for display it in the appropriate browser.

b) JSP scriptlet tag :

A JSP scriptlet lets you declare or define any java code that use into the jsp page.
scriptlet tag Start with  <% and End with %> 
The Code placed inside this tag must end with a semicolon (;).
Syntax:    <% Java Code; %> 
EX.
<% 
   int a = 10;
   out.print("a ="+a);
%>

Note :  The XML authors can use an alternative syntax for JSP scriptlet tag.
<jsp:scriptlet> 
     Java code;
</jsp:scriptlet>

c) JSP Expression tag :

A JSP expression is used to insert Java values directly into the output. 
JSP Expression tag is use for evaluating any expression and directly displays the output in appropriate web browser.
Expression tag Start with <%= and End with %> 
The Code placed inside this tag not end with a semicolon (;).
Syntax:     <%= Java Code %>
For example. the following shows the date/time that the page was requested: 
Current time: <%= new java.util.Date() %>
Note :  The XML authors can use an alternative syntax for JSP expressions: 
<jsp:expression>
  Java Expression
</jsp:expression>

d) Comment tag :

Although you can always include HTML comments in JSP pages, users can view these if they view the page's source. If you don't want users to be able to see your comments, embed them within the <%-- ... --%> tag.
 <html>
  <head>
	    <title>Comment demo</title>
  </head>
  <body>
  	<p>This is simple demo of test the comment </p>
    	<!-- This is a comment. Comments are not displayed in the browser but               displayed in source code -->
<%-- This is a comment. Comments are not displayed in the browser as well as not displayed in source code also --%>
  </body>
</html> 

Output :
This is simple demo of test the comment.
Now right click on the browser and select the view source so we can get the code in Notepad like,
 <html>
   <head>
      <title>Comment demo page</title>
   </head>
   <body>
  	<p>This is simple demo of test the comment. </p>
    	<!-- This is a comment. Comments are not displayed in the browser but displayed in source code-->
   </body>
 </html> 

In the view sorce code, the comment put in <%-- comment --%> will not display but the comment put in <!- comment -->  will display.
Now using these JSP Scripting elements, we are developing some simple examples so the use of these tags can easily understood.
 <html>
  <head>
	    <title>Scripting Demo page</title>
  </head>
  <body>
  	<%-- declaration tag --%>
    <%! int i = 10; %>
    
    <%-- scriptlet tag --%>
    <% out.print("i = "+i); %>
    <br>
    <br>
    <% out.print("for loop execution start..........."); %>
    <br>
    <% 
    for(int j=1;j<=10;j++)
    {
    	out.print("j = "+j);
    %>
    <br>
    <% 
    }
    out.print("for loop execution complete...........");
    %>
    <br>
    <br>
    
    <%-- expression tag --%>
    <%! int a = 10;
    	int b = 20; 
    %>
    The addition of two variable : a + b = 10 + 20 = <%= a+b %>
    <br>
    <br>
    Current time : <%= new java.util.Date() %>
  </body>
</html> 

Output :
i = 10 
for loop execution start........... 
j = 1 
j = 2 
j = 3 
j = 4 
j = 5 
j = 6 
j = 7 
j = 8 
j = 9 
j = 10 
for loop execution complete........... 
The addition of two variable : a + b = 10 + 20 = 30 
Current time : Sun Feb 12 14:00:35 IST 2012

- See more at: http://www.java2all.com/1/2/8/18/Technology/JSP/JSP-Elements/JSP-Scripting-element#sthash.weAmKnWO.dpuf
Read More

JSP LifeCycle

// siddhu vydyabhushana // 1 comment
The lifecycle of JSP is controlled by three methods which are automatically called when a JSP is requested and when the JSP terminates normally.

These are:
jspInit () , _jspService() , jspDestroy().

jspInit() method is identical to the init() method in a Java Servlet and in applet.

It is called first when the JSP is requested and is used to initialize objects and variables that are used throughout the life of the JSP.

_jspService() method is automatically called and retrieves a connection to HTTP.
It will call doGet or doPost() method of servlet created.

jspDestroy() method is identical to the destroy() method in Servlet.

The destroy() method is automatically called when the JSP terminates normally.
It isn’t called by JSP when it terminates abruptly.
It is used for cleanup where resources used during the execution of the JSP ate released, such as disconnecting form database.
Java2All.Com
- See more at: http://www.java2all.com/1/2/4/90/Technology/JSP/JSP-introduction/JSP-life-cycle#sthash.pJXGQLE9.dpuf
Read More

JSP Architechture

// siddhu vydyabhushana // 3 comments



  1. User requesting a JSP page through internet via web browser.
  2. The JSP request is sent to the Web Server.
  3. Web server accepts the requested .jsp file and passes the JSP file to the JSP Servlet Engine.
  4. If the JSP file has been called the first time then the JSP file is parsed otherwise servlet is instantiated.
  5. The next step is to generate a servlet from the JSP file. In that servlet file, all the HTML code is converted in println statements.
  6. Now, The servlet source code file is compiled into a class file (bytecode file).
  7. The servlet is instantiated by calling the init and service methods of the servlet’s life cycle.
  8. Now, the generated servlet output is sent via the Internet form web server to user's web browser.
  9. Now in last step, HTML results are displayed on the user's web browser.

Read More

Introduction to JSP

// siddhu vydyabhushana // 3 comments
Jsp stands for “Java server page”.

JSP are built by SUN Microsystem.

JSP enable the developers to directly insert java code into jsp file, this makes the development process very simple and its maintenance also becomes very easy.

JSP simply place JAVA inside the HTML pages means it’s an embedded of html tag and jsp’s own tag.
The extension of jsp page is ".jsp"

Java is known for its characteristic of "write once, run anywhere." and JSP page is a combination of java code into html file so we can say that the JSP pages are platform independent means .jsp pages can run in any platform.

The following example contains the simple html code.
File name: simple.html
 <html>
  <head>
     <title>web page</title>
  </head>
  <body>
    <p><b>This is a simple demo of creating a web page.</b><p> <br>
  </body>
</html> 

The output of above program is: This is a simple demo of creating a web page.
Now, just change this HTML page extension to ".jsp" instead of ".html" extension so the file name becomes “simple.jsp” and now it’s became a simple jsp file.
File name: simple.jsp
 <html>
  <head>
     <title>web page</title>
  </head>
  <body>
    <p><b>This is a simple demo of creating a web page.</b><p> <br>
  </body>
</html> 

The output of above program is: This is a simple demo of creating a web page.
NOTE : The simple .java file which is run on server side is called Servlet. For the detail of Servlet you can visit the Servlet technology in our site.
- See more at: http://www.java2all.com/1/2/4/10/Technology/JSP/JSP-introduction/Introduction-to-JSP#sthash.YOrPHvG0.dpuf
Read More

Friday 5 October 2012

Login and Registration using JSP , MS Access Database

// siddhu vydyabhushana // 3 comments
As per too many requests am going to explain detail database connection using jsp and ms access .Java server pages are dynamic so we are using it for db connections.Coming to scripting jsp is most flexible and reliable than others,but present php leading good role in web designing..................

Last Posts:

1)Oracle Database Connection 
2)MS Access Database Connection 

HTML CODE PIC FOR LOGIN :



JSP CODE FOR LOGIN.JSP:   

here we used request.getParameter   to get values from one session to another. I used try , catch method when an error occur it will goes to ctach field.





Code designed by #Siddhu Vydyabhushana Subscribe Me


<%@ page import="java.sql.*"%>
<%
try
{
String user2=request.getParameter("username");
String pass2=request.getParameter("password");

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:epoll","","");
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery("select pass from user where user='"+user2+"' and pass='"+pass2+"'");
if(rs.next())
{
String pass3=rs.getString("pass");
String flag1=rs.getString("flag");
if(flag1.equals(str)==true)
{

if (pass3.equals(pass2)==true)
{
session.putValue("user1",user2);
response.sendRedirect("user.jsp");
}
}
}
else
{%>
<script>
alert("invalid login");
response.sendRedirect("epoll.html");

</script>

<%}
con.close();
}
catch(Exception e)
{
out.println("done exception"+e);

}

HTML PIC FOR SIGNUP:-



signup.jsp code:   here we used request.getParameter   to get values from one session to another. I used try , catch method when an error occur it will goes to ctach field.



Code designed by #Siddhu Vydyabhushana Subscribe Me

<%@ page import="java.sql.*"%>
<%
try
{
String user1=request.getParameter("username");
String email1=request.getParameter("email");
String pass1=request.getParameter("password");
int flag=0;
String str=Integer.toString(flag);
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:epoll","","");
PreparedStatement pst= con.prepareStatement("insert into user values(?,?,?,?)");
pst.setString(1,user1);
pst.setString(2,email1);
pst.setString(3,pass1);
pst.setString(4,str);

pst.executeUpdate();

con.close();
}
catch(Exception e)
{
out.println("done exception"+e);

}
%>

Read More

Tuesday 2 October 2012

Simple Disappear effect using jquery

// siddhu vydyabhushana // Leave a Comment


Now let us see a new concept in Jquery

This sample will show you how to make something disappear when an image button is clicked.

Required Files:

1. disappear.html
2.jquery-1.4.2.js
sample


When the <img class="delete"> is clicked, it will find its parent element <div class="pane"> and animate its opacity=hide with slow speed.

Here i am placing source code... 

DOWNLOAD SOURCE CODE

DOWNLOAD jquery-1.4.2.js


Read More

Auto Complete Text box using jquery with oracle database and JSP

// siddhu vydyabhushana // 3 comments


Required Files:

  • index.jsp
  • list.jsp
  • jquery.autocomplete.js
  • jquery-1.4.2.min.js
  • style.css




Database:


create a user with name "ex" and password as "ex"create a table as "states" place all your required states in that table



Source Files:

Here i am giving you a link to DOWNLOAD these five files 


How to run:

copy these five files and paste it into C:\Program Files\Apache Software Foundation\Tomcat 7.0\webapps\yourfolder
run it as http://localhost:8001/index.jsp
type just one character like 'a' then you find all the states starts with "a".


How the source code is working:

See in list.jsp line no.33 you may get one doubt, what is that ‘q‘ right ? in fact we are not sending any parameter with name ‘q’ from the index.jsp but for every keyup in the text box (in index.jsp) jquery will sends each character to the list.jsp in the form of ‘q’ and compares with the values in that list, i mean ‘q’ is the default parameter using by jQuery API.


How to send extra parameters along with that q:

$(‘#country’).autocomplete(“list.jsp”, {extraParams: {state: California }} );
This will generates the internal URL like
……./getdata.jsp?q=ourChar&state=California

Output:

Database:

Table name: states



Read More

Monday 1 October 2012

Connect Oracle10x Database using JSP

// siddhu vydyabhushana // Leave a Comment
As per requesting am going to show how to connect Oracle10g using JSP.
Before u nedd to download and install it 

Download: http://www.oracle.com/technetwork/database/enterprise-edition/downloads/112010-win32soft-098987.html

using Above link first download and install it . after first configuration is needed,for that 

START----->My Computer----->Os Drive (C or D)-------> Programming Files-----

-->Oracle----->Product--->10.2.0.----->server----->Jdbc----->lib

copy the path as below

C:\oraclexe\app\oracle\product\10.2.0\server\jdbc\lib\ojdbc.jar;.;

copy the above code and paste it in EnvironMent Variables


Goto Right Click on My Computer----->Properties------>Advanced System 

Settings------->Environment Variables-----> Find Path in System Variables the

and paste the above in path field if there is any path u use ; (Semicolon) to

differentiate

for eg:


D:\ProgramFiles\Java\jdk1.6.0\bin  ;

C:\oraclexe\app\oracle\product\10.2.0\server\jdbc\lib\ojdbc.jar;.;





JSP CODE:

just for demo am showing this in further post i will give brief info....

Class.Forname:

               Class.forName("oracle.jdbc.driver.OracleDriver");

Connection: 
for creating connection b/w user and database in jsp we need to write this type of code ,we are getting connection for decvice driver for that we used 
DriverManager.getConnection

we are getting connection using URL:- jdbc:oracle:thin:@localhost:1521:XE

On installation we gave Username and Passwor d to Protect ur DB.




Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:15 21:XE","Username","Password");
Read More