Você está na página 1de 13

What is Struts?

Struts Frame work is the implementation of Model-View-Controller (MVC) design


pattern
for the JSP. Struts is maintained as a part of Apache Jakarta project and is open source.
Struts Framework is suited for the application of any size. Latest version of struts can be
downloaded from http://jakarta.apache.org/. We are using jakarta-struts-1.1 and jakarta-
tomcat-5.0.4 for this tutorial.

What is Model-View-Controller (MVC) Architecture?


Model-View-Controller architecture is all about dividing application components into
three different categories Model, View and the Controller. Components of the MVC
architecture has unique responsibility and each component is independent of the other
component. Changes in one component will have no or less impact on other
component. Responsibilities of the components are:
Model: Model is responsible for providing the data from the database
and saving the data into the data store. All the business logic are implemented in the
Model. Data entered by the user through View are check in the model before saving into
the database. Data access, Data validation and the data saving logic are part of Model.
View: View represents the user view of the application and is responsible for taking
the input from the user, dispatching the request to the controller and then receiving
response from the controller and displaying the result to the user. HTML, JSPs
, Custom Tag Libraries and Resources files are the part of view component.
Controller: Controller is intermediary between Model and View. Controller is
responsible for receiving the request from client. Once request is received from client it
executes the appropriate business logic from the Model and then produce the output to
the user using the View component. ActionServlet, Action, ActionForm and struts-
config.xml are the part of Controller.

1. Java Server Pages or JSP for short is Sun's solution for developing dynamic web
sites. JSP provide excellent server side scripting support for creating database

driven web applications.

2. JSP Tutorials - Introducing Java Server Pages Technology


JavaServer Pages (JSP) technology is the Java platform technology for delivering
dynamic content to web clients in a portable, secure and well-defined way.
1. JPS pages are high level extension of servlet and it enable the developers

to embed java code in html pages. JSP files are finally compiled into a servlet by the JSP
engine. Compiled servlet is used by the engine to serve the requests.

<%@directive attribute="value" %>


Where directive may be:

1. page: page is used to provide the information about it.


Example: <%@page language="java" %>

2. include: include is used to include a file in the JSP page.


Example: <%@ include file="/header.jsp" %>

3. taglib: taglib is used to use the custom tags

in the JSP pages (custom tags allows us to defined our own tags).
Example: <%@ taglib uri="tlds/taglib.tld" prefix="mytag" %>
and attribute may be:

1. language="java"
This tells the server

that the page is using the java language. Current JSP specification supports only
java language.
Example: <%@page language="java" %>

2. extends="mypackage.myclass"
This attribute is used when we want to extend any class. We can use comma(,) to
import more than one packages.
Example: <%@page language="java" import="java.sql.*,mypackage.myclass"
%>

3. session="true"
When this value is true session data is available to the JSP page otherwise not. By
default this value is true.
Example: <%@page language="java" session="true" %>

4. errorPage="error.jsp"
errorPage is used to handle the un-handled exceptions in the page.
Example: <%@page language="java" session="true" errorPage="error.jsp" %>

5. contentType="text/html;charset=ISO-8859-1"
Use this attribute to set the mime type and character set of the JSP.
Example: <%@page language="java" session="true"
contentType="text/html;charset=ISO-8859-1" %>

1. Scriplets
In this tag we can insert any amount of valid java code and these codes are placed
in _jspService method by the JSP engine.

2. Expressions
We can use this tag to output any data on the generated page. These data are
automatically converted to string and printed on the output stream.

What is JSP Actions?


Servlet container provides many built in functionality to ease the development of the
applications. Programmers
can use these functions in JSP applications. The JSP Actions tags enables the programmer
to use these functions. The JSP Actions are XML tags
that can be used in the JSP page.
Here is the list of JSP Actions:

• jsp:include
The jsp:include action work as a subroutine, the Java servlet

temporarily passes the request and response to the specified JSP/Servlet. Control
is then returned back to the current JSP page.

• jsp:param
The jsp:param action is used to add the specific parameter to current request.
The jsp:param tag can be used inside a jsp:include, jsp:forward or jsp:params
block.

• jsp:forward
The jsp:forward tag is used to hand off the request and response to another JSP
or servlet. In this case the request never return to the calling JSP page.

• jsp:plugin
In older versions of Netscape Navigator and Internet Explorer; different tags is
used to embed applet. The jsp:plugin tag actually generates the appropriate
HTML code the embed the Applets correctly.

• jsp:fallback
The jsp:fallback tag is used to specify the message to be shown on the browser if
applets is not supported by browser.
Example:
<jsp:fallback>
<p>Unable to load applet</p>
</jsp:fallback>

• jsp:getProperty
The jsp:getPropertyB is used to get specified property from the JavaBean object.

• jsp:setProperty
The jsp:setProperty tag is used to set a property in the JavaBean object.

• jsp:useBean
The jsp:useBean tag is used to instantiate an object of Java Bean or it can re-use
existing java bean object.

INTRODUCTION TO JSP SCRIPTLETS


Syntax of JSP Scriptles are:
<%
//java codes
%>
JSP Scriptlets begins with <% and ends %> .We can embed any amount of java code in
the JSP Scriptlets. JSP Engine places these code in the _jspService() method. Variables
available to the JSP Scriptlets are:

• request:
request represents the clients request and is a subclass of HttpServletRequest. Use
this variable to retrieve the data submitted along the request.
Example:
<%
//java codes
String userName=null;
userName=request.getParameter("userName");
%>
• response:
response is subclass of HttpServletResponse.

• session:
session represents the HTTP session object associated with the request.

• out:
out is an object of output stream and is used to send any output to the client.

Other variable available to the scriptlets are pageContext, application,config and


exception.

INTRODUCTION TO JSP EXPRESSIONS


Syntax of JSP Expressions are:
<%="Any thing" %>
JSP Expressions start with
Syntax of JSP Scriptles are with <%= and ends with %>. Between these this you can
put anything and that will converted to the String and that will be displayed.
Example:
<%="Hello World!" %>
Above code will display 'Hello World!'.

Example
Till now you learned about the JSP syntax, now I will show you how to create a simple
dynamic JSP page that prints the current date and time. So the following code
accomplish this:

<%@page contentType="text/html" import="java.util.*" %>


<!--
http://www.roseindia.net/jsp
-->
<html>
<body>
<p>&nbsp;</p>
<div align="center">
<center>
<table border="0" cellpadding="0" cellspacing
="0" width="460" bgcolor="#EEFFCA">
<tr>
<td width="100%"><font size="6" color
="#008000">&nbsp;Date Example</font></td>
</tr>
<tr>
<td width="100%"><b>&nbsp;Current Date
and time is:&nbsp; <font color="#FF0000">

<%= new java.util.Date() %>


</font></b></td>
</tr>
</table>
</center>
</div>
</body>
</html>

The heart of this example is Date() function of the java


.util package which returns the current data and time.
In the JSP Declaratives
<%@page contentType="text/html" import="java.util.*" %>
we are importing the java.util package and following JSP Expression code
<%= new java.util.Date() %>

Reading Request Information


When an HTTP client such as web browser sends a request to a wen server, along with
the request it also sends some HTTP variables like Remote address, Remote host,
Content type etc. In some cases these variables are useful to the programmers
. So here is the code of the jsp file which prints the HTTP request information:

<%@page contentType="text/html" import="java.util.*" %>


<!--
http://www.roseindia.net/jsp
-->
<html>
<body>
<p><font size="5" color="#800000">Request
Information:</font></p>
<div align="left">
<table border="0" cellpadding="0" cellspacing="0" width="70%"
bgcolor="#EEFFCA">
<tr>
<td width="33%"><b><font color="#800000">Request
Method:</font></b></td>
<td width="67%"><font
color="#FF0000"><%=request.getMethod()%></font></td>
</tr>
<tr>
<td width="33%"><b><font color="#800000">Request
URI:</font></b></td>
<td width="67%"><font
color="#FF0000"><%=request.getRequestURI()%></font></td>
</tr>
<tr>
<td width="33%"><b><font color="#800000">Request
Protocol:</font></b></td>
<td width="67%"><font
color="#FF0000"><%=request.getProtocol()%></font></td>
</tr>
<tr>
<td width="33%"><b><font color="#800000">Path
Info:</font></b></td>
<td width="67%"><font
color="#FF0000"><%=request.getPathInfo()%></font></td>
</tr>
<tr>
<td width="33%"><b><font color="#800000">Path
translated:</font></b></td>
<td width="67%"><font
color="#FF0000"><%=request.getPathTranslated()%></font></td
>
</tr>
<tr>
<td width="33%"><b><font color="#800000">Query
String:</font></b></td>
<td width="67%"><font
color="#FF0000"><%=request.getQueryString()%></font></td>
</tr>
<tr>
<td width="33%"><b><font color="#800000">Content
length:</font></b></td>
<td width="67%"><font
color="#FF0000"><%=request.getContentLength()%></font></td
>
</tr>
<tr>
<td width="33%"><b><font color="#800000">Content
type:</font></b></td>
<td width="67%"><font
color="#FF0000"><%=request.getContentType()%></font></td>
</tr>
<tr>
<td width="33%"><b><font color="#800000">Server
name:</font></b></td>
<td width="67%"><font
color="#FF0000"><%=request.getServerName()%></font></td>
</tr>
<tr>
<td width="33%"><b><font color="#800000">Server
port:</font></b></td>
<td width="67%"><font
color="#FF0000"><%=request.getServerPort()%></font></td>
</tr>
<tr>
<td width="33%"><b><font color="#800000">Remote
user:</font></b></td>
<td width="67%"><font
color="#FF0000"><%=request.getRemoteUser()%></font></td>
</tr>
<tr>
<td width="33%"><b><font color="#800000">Remote
address:</font></b></td>
<td width="67%"><font
color="#FF0000"><%=request.getRemoteAddr()%></font></td>
</tr>
<tr>
<td width="33%"><b><font color="#800000">Remote
host:</font></b></td>
<td width="67%"><font
color="#FF0000"><%=request.getRemoteHost()%></font></td>
</tr>
<tr>
<td width="33%"><b><font color="#800000">Authorization
scheme:</font></b></td>
<td width="67%"><font
color="#FF0000"><%=request.getAuthType()%></font></td>
</tr>
</table>
</div>
</body>
</html>

Retrieving the data posted to a JSP file


from HTML file

Now I will show you how to retrieve the data posted from a HTML file in a JSP page.
Consider an html page that prompts the user to enter his/her name, let's call it
getname.htm. Here is the code of the html file:

<html>
<head>
<title>Enter your name</title>
</head>
<body>
<p>&nbsp;</p>
<form method="POST"
action="showname.jsp">
<p><font color="#800000" size="5">Enter
your name:</font><input type
="text" name="username" size="20"></p>

<p><input type="submit" value="Submit"


name="B1"></p>
</form>
</body>
</html>

The target of form is "showname.jsp", which displays the name entered by the user. To
retrieve the value entered by the user we uses the
request.getParameter("username");
code.

Here is the code of "showname.jsp" file:

<%@page
contentType="text/html" %>
<!--
http://www.roseindia.net/jsp
-->
<html>
<body>
<p><font size="6">Welcome :&nbsp;
<%=request.getParam
eter("username")%></font></p>
</body>
</html>
need popup calender
Hi, I need a pop up calender Accessing database from JSP
code in html, but i guess In This article I am going to discuss the
javascript would be needed connectivity from MYSQL database with
for event handling . I need a JSP.we take a example of Books
co database. This database contains a
table named books_details.
 project guidance
i have to make a project
on resume management
Fill the book name and author fields and press Submit button. A page will open and
wherein a company can authors like...
select a candidaite based
on the data fed by him on
 change password
servlets
hi all, i need the codes to
create a change password
servlet.
 how to produce halftone
shares?
Respected sir, I'm a B.E
final year student doing
my project in halftone
visual cryptography.
 open source
hi! what is open source? i
heard that SUN has
released open source
.what is re requisite to
understand that open
sourc
 converting image to
byte[]
Dear Sir Can you please
tell me a solution for
converting a
java.awt.Image to byte[]
eg: java.awt.Image
 java
java TestClass WHAT IS
THE ARGUMENT
PROVIDE BY JVM
WHILE WE RUN A
CLASS
FILE.DEFINATELY
NOT NULL.THEN
WHAT IS IT.
 JSP
Hi! I am doing a project
in JSP called "SMS
schedule Reminder"... In
this i have to store the
user schedules and i h
 Connection.close() issue
Hi, I connected to
MySQL with InnoDB as
a database through My
JDBC Application. Once

Você também pode gostar