Wednesday, February 20, 2008

Uploading a file in JSP

uploadFile.html...........


< body>

< !-- some codes... -->

< method="post" action="upload.jsp" name="uploadForm" enctype="'multipart/form-data'">
< !-- please notice (ENCTYPE='multipart/form-data').... -->

< input name="uploadfile" type="file">
< p>
< input name="Submit" value="Upload" type="submit">
< input name="Reset" value="Reset" type="reset">
< input name="action" value="upload" type="hidden">
< /p>


< !-- some codes... -->

< /body>


upload.jsp.............



<%@ page import="java.io.*"%>
<% String contentType = request.getContentType(); if ((contentType != null) && (contentType.indexOf("multipart/form-data") >= 0)) {
DataInputStream in = new DataInputStream(request.getInputStream());
int formDataLength = request.getContentLength();

byte dataBytes[] = new byte[formDataLength];
int byteRead = 0;
int totalBytesRead = 0;
while (totalBytesRead < byteread =" in.read(dataBytes," file =" new" savefile =" file.substring(file.indexOf(" filename="\" savefile =" saveFile.substring(0," savefile =" saveFile.substring(saveFile.lastIndexOf(" lastindex =" contentType.lastIndexOf(" boundary =" contentType.substring(lastIndex" pos =" file.indexOf(" filename="\" pos =" file.indexOf(" pos =" file.indexOf(" pos =" file.indexOf(" boundarylocation =" file.indexOf(boundary," startpos =" ((file.substring(0," endpos =" ((file.substring(0," filepath="session.getServletContext().getRealPath(" fileout =" new">

***important>>notice
request.getContentType()
request.getInputStream()
request.getContentLength()

Monday, February 18, 2008

Getting Client Info..

< body>
--some codes go here

Client computer details:

< br>< br>

< b>Ip address< /b>:

< br>

<%=request.getRemoteAddr()%>

< br>< br>

< b>Computer name< /b>:

< br>

<%=request.getRemoteHost()%>

< br>< br>

--some codes go here

< /body>



*****important>>notice
request.getRemoteAddr()
request.getRemoteAddr()
methods

Sending a mail....

(1)method-Using JSPs and Java Mail API


Code from SendMail.jsp

< id="sendMail" class="SendMailBean" scope="page">
<% String a = request.getParameter("p_from"); String b = request.getParameter("p_to"); String c = request.getParameter("p_cc"); String d = request.getParameter("p_subject"); String e = request.getParameter("p_message"); %>
< %= sendMail.send(a, b, c, d, e) %>



(2)method-Using the sendMail utility tag

Code from SendMail.jsp

...

<% //Read all inputs into local variables String from = request.getParameter("p_from"); String to = request.getParameter("p_to"); String cc = request.getParameter("p_cc"); String bcc = request.getParameter("p_bcc"); String subject = request.getParameter("p_subject"); String message = request.getParameter("p_message"); String smtpSvr = request.getParameter("p_smtpServer"); session.setAttribute("smtpServer",l_smtpSvr); %>

< %-- Use OC4J Jsp mail tag to send the mail --%>

< host="'<%="(String)session.getValue(">'
sender='<%=from%>'
recipient='<%=to%>'
cc='<%=cc%>'
bcc='<%=bcc%>'
subject='<%=subject%>'>
<%=message%>
< /mail:sendMail>
...


also try this Click

Creating & Processing a Form using JSP

lets build the 'HTML form' first..

***myform.jsp

< body>

---some other codes

< form action="myformconfirm.jsp" method="post">

Enter in a website name:< br>

< input type="text" name="website">< br>

< input type="submit" value="Submit this" name="submit" />

< /form>

---some other codes
< /body>


***myformconfirm.jsp <<<

< body>

Your info has been received:

< br>< br>

<%

String sName = request.getParameter("website");

out.print(sName);

%>

< /body>


***important>>

notice the use of request.getParameter() method..

Javabeans for JSP ans Scopes

special type of class that has a number of methods.
JSP page can call these methods..
Javabean could be used in another JSP page (promoting reuse).

__To use a Javabean in a JSP page use the following syntax:

< id = " ...." scope = "application" class = "com...">

** scopes:

page - valid until page completes.

request - bean instance lasts for the client request

session - bean lasts for the client session.

application - bean instance created and lasts until application ends.


more about scopes.....



Application scope

Application scope is the broadest scope and should only be used when necessary. You can

create objects bound at application level in JSPs that are not session-aware. You can also

use application-bound objects to share data among different sessions of the same

application. When you no longer need objects bound to an application, you should explicitly

remove them to free up memory resources.



Session scope

In my experience, session scope is more commonly used than application scope. Session scope

allows you to create and bind objects to a SESSION. You must create objects bound to the

session in session-aware JSPs and make them available to all JSPs and servlets in the same

session. Session scope is often used for managing security credentials and for managing

state among multiple pages. As with application scope,

objects bound to session scope should be explicitly removed when no longer needed.



Request scope

I use request scope most often for binding created objects. Objects created and bound to

request scope are available to pages in that same request. These objects are automatically

released from memory when request processing completes. This is advantageous because

explicit cleanup is not required and there is less risk of burdening the system with

needless memory consumption.



Page scope

You should use page scope for objects created only for the current page. Like request scope,

the page scope does not require explicit removal of created and bound objects. I rarely use

page scope in my JSP applications, but it is the action's default scope.

**Using JSP tags

There are five main tags:


1. Declaration tag

2. Expression tag

3. Directive tag

4. Scriptlet tag

5. Action tag


1*Declaration tag

<%! %>

*To declare variables or methods.

*Do not generate output so are used with JSP expressions or scriptlets.
eg:
<%!
private int counter = 0 ;

private String get Account ( int accountNo) ;
%>


2*Expression tag

<%= %>

*To embed any Java expression
*It is short for out.println()
*semicolon ( ; ) does not appear at the end of the code
eg;

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


3*Directive tag

<%@ directive ... %>

*To gives special information about the page to the JSP Engine
*three main types of directives:
1)page - processing information for this page.

2)Include - files to be included.

3)Tag library - tag library to be used in this page.


(1)Page directive--optional attributes that provide the
JSP Engine with special processing information



language--Which language the file uses-- <%@ page language = "java" %>


extends--Superclass used by the JSP engine for the translated Servlet.
< %@ page extends = "com.taglib... %>


import-- Import all the classes in a java package into the current JSP page.
This allows the JSP page to use other java classes.
<%@ page import = "java.util.*" %>


info-- Developer uses info attribute to add information/document for a page.
Typically used to add author,version,copyright and date info.
<%@ page info = "http://best-jsp.blogspot.com/,copyright 2008. " %>



(2)Include directive-to include contents of a file inside another
eg:

<%@ include file = "include/privacy.html" %>
or
<%@ include file = "navigation.jsp" %>



4*Scriptlet tag

<% ... %>

*to emmbed any valid Java code is called a Scriptlet
eg:

<%
String username = "BA is the Greatest !" ;

out.println ( username ) ;

%>

Sunday, February 17, 2008

doing jsp with 3 files

3 kind of pages

(1)index.jsp.............


<>
<>
< equiv="Content-Type" content="text/html; charset=UTF-8">
<>JSP Page< /title>
< /head>
<>
<>Entry Form..< /h2>< name=" Name Input Form" action="response.jsp">
Enter your name:
< type="text" name="name" value="">
< type="submit" value="OK">
< /form>
< /body>
< /html>


(2)responsing JSP file(response.jsp)

<>
<>
< equiv="Content-Type" content="text/html; charset=UTF-8">
<>JSP Page< /title>
< /head>
<>
< id="mybean" scope="session" class="org.me.hello.NameHandler">
< name="mybean" property="name">
<>Hello, !< /h2>
< /body>
< /html>



(3)

package org.me.hello;


public class NameHandler {

private String name;
public NameHandler(){
name = null;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

.......
ok Try with Netbeans6

first jsp

**JSP simply puts Java inside HTML pages.
You can take any existing HTML page and change
its extension to ".jsp" instead of ".html".


**what makes JSP useful is the ability to embed Java.
< HTML>
< BODY>
Hello! The time is now < %= new java.util.Date() %>
< /BODY>
< /HTML>

**The character sequences < %= and %> enclose Java expressions,
which are evaluated at run time.