Hello deveplopers,
Thanks for the last response is greate, i must confess that, your answer are helping us alot. thanks.
Please, how can i use JSF code of this Page1.java that's, when i click button it will submit to database.
This is the table of database i want to use:
CREATE TABLE UPLOAD(
NAME VARCHER(20),
ADDRESS VARCHAR(30),
CITY VARCHAR(20),
PICTURE IMAGE
)
I am using ODBC to conect the database that's:
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver")
Connection con=DriverManager.getConnection("jdbc:odbc:UploadSource", "", "")
THIS IS THE CODE I CREATED IN NETBEANS VISUAL PACK
/*
* Page1.java
*
* Created on November 29, 2007, 9:30 AM
* Copyright kelly
*/
package fileuploadexample;
import com.sun.rave.web.ui.appbase.AbstractPageBean;
import com.sun.rave.web.ui.component.Body;
import com.sun.rave.web.ui.component.Button;
import com.sun.rave.web.ui.component.Form;
import com.sun.rave.web.ui.component.Head;
import com.sun.rave.web.ui.component.Html;
import com.sun.rave.web.ui.component.ImageComponent;
import com.sun.rave.web.ui.component.Label;
import com.sun.rave.web.ui.component.Link;
import com.sun.rave.web.ui.component.MessageGroup;
import com.sun.rave.web.ui.component.Page;
import com.sun.rave.web.ui.component.StaticText;
import com.sun.rave.web.ui.component.TextField;
import com.sun.rave.web.ui.component.Upload;
import com.sun.rave.web.ui.model.UploadedFile;
import java.io.File;
import javax.faces.FacesException;
import javax.faces.component.html.HtmlCommandButton;
import javax.servlet.ServletContext;
/**
*
Page bean that corresponds to a similarly named JSP page. This
* class contains component definitions (and initialization code) for
* all components that you have defined on this page, as well as
* lifecycle methods and event handlers where you may add behavior
* to respond to incoming events.
*/
public class Page1 extends AbstractPageBean {
//
private int __placeholder;
/**
*
Automatically managed component initialization. WARNING:
* This method is automatically generated, so any user-specified code inserted
* here is subject to being replaced.
*/
private void _init() throws Exception {
}
private Page page1 = new Page();
public Page getPage1() {
return page1;
}
public void setPage1(Page p) {
this.page1 = p;
}
private Html html1 = new Html();
public Html getHtml1() {
return html1;
}
public void setHtml1(Html h) {
this.html1 = h;
}
private Head head1 = new Head();
public Head getHead1() {
return head1;
}
public void setHead1(Head h) {
this.head1 = h;
}
private Link link1 = new Link();
public Link getLink1() {
return link1;
}
public void setLink1(Link l) {
this.link1 = l;
}
private Body body1 = new Body();
public Body getBody1() {
return body1;
}
public void setBody1(Body b) {
this.body1 = b;
}
private Form form1 = new Form();
public Form getForm1() {
return form1;
}
public void setForm1(Form f) {
this.form1 = f;
}
private Label label1 = new Label();
public Label getLabel1() {
return label1;
}
public void setLabel1(Label l) {
this.label1 = l;
}
private Label label2 = new Label();
public Label getLabel2() {
return label2;
}
public void setLabel2(Label l) {
this.label2 = l;
}
private TextField tname = new TextField();
public TextField getTname() {
return tname;
}
public void setTname(TextField tf) {
this.tname = tf;
}
private Label label3 = new Label();
public Label getLabel3() {
return label3;
}
public void setLabel3(Label l) {
this.label3 = l;
}
private TextField taddrs = new TextField();
public TextField getTaddrs() {
return taddrs;
}
public void setTaddrs(TextField tf) {
this.taddrs = tf;
}
private Label label4 = new Label();
public Label getLabel4() {
return label4;
}
public void setLabel4(Label l) {
this.label4 = l;
}
private TextField tcity = new TextField();
public TextField getTcity() {
return tcity;
}
public void setTcity(TextField tf) {
this.tcity = tf;
}
private Label label5 = new Label();
public Label getLabel5() {
return label5;
}
public void setLabel5(Label l) {
this.label5 = l;
}
private Upload fileUpload1 = new Upload();
public Upload getFileUpload1() {
return fileUpload1;
}
public void setFileUpload1(Upload u) {
this.fileUpload1 = u;
}
private Button uploadFileButton = new Button();
public Button getUploadFileButton() {
return uploadFileButton;
}
public void setUploadFileButton(Button b) {
this.uploadFileButton = b;
}
private Label label6 = new Label();
public Label getLabel6() {
return label6;
}
public void setLabel6(Label l) {
this.label6 = l;
}
private StaticText fileNameStaticText = new StaticText();
public StaticText getFileNameStaticText() {
return fileNameStaticText;
}
public void setFileNameStaticText(StaticText st) {
this.fileNameStaticText = st;
}
private Label label7 = new Label();
public Label getLabel7() {
return label7;
}
public void setLabel7(Label l) {
this.label7 = l;
}
private StaticText fileTypeStaticText = new StaticText();
public StaticText getFileTypeStaticText() {
return fileTypeStaticText;
}
public void setFileTypeStaticText(StaticText st) {
this.fileTypeStaticText = st;
}
private Label label8 = new Label();
public Label getLabel8() {
return label8;
}
public void setLabel8(Label l) {
this.label8 = l;
}
private StaticText fileSizeStaticText = new StaticText();
public StaticText getFileSizeStaticText() {
return fileSizeStaticText;
}
public void setFileSizeStaticText(StaticText st) {
this.fileSizeStaticText = st;
}
private ImageComponent image1 = new ImageComponent();
public ImageComponent getImage1() {
return image1;
}
public void setImage1(ImageComponent ic) {
this.image1 = ic;
}
private HtmlCommandButton button2 = new HtmlCommandButton();
public HtmlCommandButton getButton2() {
return button2;
}
public void setButton2(HtmlCommandButton hcb) {
this.button2 = hcb;
}
private HtmlCommandButton button3 = new HtmlCommandButton();
public HtmlCommandButton getButton3() {
return button3;
}
public void setButton3(HtmlCommandButton hcb) {
this.button3 = hcb;
}
private Label label9 = new Label();
public Label getLabel9() {
return label9;
}
public void setLabel9(Label l) {
this.label9 = l;
}
private MessageGroup messageGroup1 = new MessageGroup();
public MessageGroup getMessageGroup1() {
return messageGroup1;
}
public void setMessageGroup1(MessageGroup mg) {
this.messageGroup1 = mg;
}
//
/**
*
Construct a new Page bean instance.
*/
public Page1() {
}
/**
*
Callback method that is called whenever a page is navigated to,
* either directly via a URL, or indirectly via page navigation.
* Customize this method to acquire resources that will be needed
* for event handlers and lifecycle methods, whether or not this
* page is performing post back processing.
*
*
Note that, if the current request is a postback, the property
* values of the components do not represent any
* values submitted with this request. Instead, they represent the
* property values that were saved for this view when it was rendered.
*/
private String realImageFilePath;
private static final String IMAGE_URL = "/resources/image-file";
public void init() {
// Perform initializations inherited from our superclass
super.init();
// Perform application initialization that must complete
// *before* managed components are initialized
// TODO - add your own initialiation code here
//
// Initialize automatically managed components
// *Note* - this logic should NOT be modified
try {
_init();
} catch (Exception e) {
log("Page1 Initialization Failure", e);
throw e instanceof FacesException ? (FacesException) e: new FacesException(e);
}
//
// Perform application initialization that must complete
// *after* managed components are initialized
// TODO - add your own initialization code here
ServletContext theApplicationsServletContext =
(ServletContext) this.getExternalContext().getContext();
this.realImageFilePath = theApplicationsServletContext.getRealPath(IMAGE_URL);
}
/**
*
Callback method that is called after the component tree has been
* restored, but before any event processing takes place. This method
* will only be called on a postback request that
* is processing a form submit. Customize this method to allocate
* resources that will be required in your event handlers.
*/
public void preprocess() {
}
/**
*
Callback method that is called just before rendering takes place.
* This method will only be called for the page that
* will actually be rendered (and not, for example, on a page that
* handled a postback and then navigated to a different page). Customize
* this method to allocate resources that will be required for rendering
* this page.
*/
public void prerender() {
String uploadedFileName = (String)
this.fileNameStaticText.getValue();
if ( uploadedFileName != null ) {
image1.setUrl(IMAGE_URL);
}
}
/**
*
Callback method that is called after rendering is completed for
* this request, if init() was called (regardless of whether
* or not this was the page that was actually rendered). Customize this
* method to release resources acquired in the init(),
* preprocess(), or prerender() methods (or
* acquired during execution of an event handler).
*/
public void destroy() {
}
/**
*
Return a reference to the scoped data bean.
*/
protected SessionBean1 getSessionBean1() {
return (SessionBean1)getBean("SessionBean1");
}
/**
*
Return a reference to the scoped data bean.
*/
protected ApplicationBean1 getApplicationBean1() {
return (ApplicationBean1)getBean("ApplicationBean1");
}
/**
*
Return a reference to the scoped data bean.
*/
protected RequestBean1 getRequestBean1() {
return (RequestBean1)getBean("RequestBean1");
}
public String uploadFileButton_action() {
// TODO: Process the button click action. Return value is a navigation
// case name where null will return to the same page.
UploadedFile uploadedFile = fileUpload1.getUploadedFile();
String uploadedFileName = uploadedFile.getOriginalName();
// Some browsers return complete path name, some don't
// Make sure we only have the file name
// First, try forward slash
int index = uploadedFileName.lastIndexOf('/');
String justFileName;
if ( index >= 0) {
justFileName = uploadedFileName.substring( index + 1 );
} else {
// Try backslash
index = uploadedFileName.lastIndexOf('\\');
if (index >= 0) {
justFileName = uploadedFileName.substring( index + 1 );
} else {
// No forward or back slashes
justFileName = uploadedFileName;
}
}
this.fileNameStaticText.setValue(justFileName);
Long uploadedFileSize = new Long(uploadedFile.getSize());
this.fileSizeStaticText.setValue(uploadedFileSize);
String uploadedFileType = uploadedFile.getContentType();
this.fileTypeStaticText.setValue(uploadedFileType);
if ( uploadedFileType.equals("image/jpeg")
|| uploadedFileType.equals("image/pjpeg")
|| uploadedFileType.equals("image/gif")
|| uploadedFileType.equals("image/png")
|| uploadedFileType.equals("image/x-png")) {
try {
File file = new File(this.realImageFilePath);
uploadedFile.write(file);
} catch (Exception ex) {
error("Cannot upload file: " + justFileName);
}
} else {
error("You must upload a JPEG, PJPEG, GIF, PNG, or X-PNG file.");
new File(this.realImageFilePath).delete();
}
return null;
}
public String button3_action() {
// TODO: Process the button click action. Return value is a navigation
// case name where null will return to the same page.
return null;
}
public String button2_action() {
// TODO: Process the button click action. Return value is a navigation
// case name where null will return to the same page.
return null;
}
}
/*
* ApplicationBean1.java
*
* Created on November 29, 2007, 9:30 AM
* Copyright kelly
*/
package fileuploadexample;
import com.sun.rave.web.ui.appbase.AbstractApplicationBean;
import javax.faces.FacesException;
/**
*
Application scope data bean for your application. Create properties
* here to represent cached data that should be made available to all users
* and pages in the application.
*
*
An instance of this class will be created for you automatically,
* the first time your application evaluates a value binding expression
* or method binding expression that references a managed bean using
* this class.
*/
public class ApplicationBean1 extends AbstractApplicationBean {
//
private int __placeholder;
/**
*
Automatically managed component initialization. WARNING:
* This method is automatically generated, so any user-specified code inserted
* here is subject to being replaced.
*/
private void _init() throws Exception {
}
//
/**
*
Construct a new application data bean instance.
*/
public ApplicationBean1() {
}
/**
*
This method is called when this bean is initially added to
* application scope. Typically, this occurs as a result of evaluating
* a value binding or method binding expression, which utilizes the
* managed bean facility to instantiate this bean and store it into
* application scope.
*
*
You may customize this method to initialize and cache application wide
* data values (such as the lists of valid options for dropdown list
* components), or to allocate resources that are required for the
* lifetime of the application.
*/
public void init() {
// Perform initializations inherited from our superclass
super.init();
// Perform application initialization that must complete
// *before* managed components are initialized
// TODO - add your own initialiation code here
//
// Initialize automatically managed components
// *Note* - this logic should NOT be modified
try {
_init();
} catch (Exception e) {
log("ApplicationBean1 Initialization Failure", e);
throw e instanceof FacesException ? (FacesException) e: new FacesException(e);
}
//
// Perform application initialization that must complete
// *after* managed components are initialized
// TODO - add your own initialization code here
}
/**
*
This method is called when this bean is removed from
* application scope. Typically, this occurs as a result of
* the application being shut down by its owning container.
*
*
You may customize this method to clean up resources allocated
* during the execution of the init() method, or
* at any later time during the lifetime of the application.
*/
public void destroy() {
}
/**
*
Return an appropriate character encoding based on the
* Locale defined for the current JavaServer Faces
* view. If no more suitable encoding can be found, return
* "UTF-8" as a general purpose default.
*
*
The default implementation uses the implementation from
* our superclass, AbstractApplicationBean.
*/
public String getLocaleCharacterEncoding() {
return super.getLocaleCharacterEncoding();
}
}
/*
* RequestBean1.java
*
* Created on November 29, 2007, 9:30 AM
* Copyright kelly
*/
package fileuploadexample;
import com.sun.rave.web.ui.appbase.AbstractRequestBean;
import javax.faces.FacesException;
/**
*
Request scope data bean for your application. Create properties
* here to represent data that should be made available across different
* pages in the same HTTP request, so that the page bean classes do not
* have to be directly linked to each other.
*
*
An instance of this class will be created for you automatically,
* the first time your application evaluates a value binding expression
* or method binding expression that references a managed bean using
* this class.
*/
public class RequestBean1 extends AbstractRequestBean {
//
private int __placeholder;
/**
*
Automatically managed component initialization. WARNING:
* This method is automatically generated, so any user-specified code inserted
* here is subject to being replaced.
*/
private void _init() throws Exception {
}
//
/**
*
Construct a new request data bean instance.
*/
public RequestBean1() {
}
/**
*
This method is called when this bean is initially added to
* request scope. Typically, this occurs as a result of evaluating
* a value binding or method binding expression, which utilizes the
* managed bean facility to instantiate this bean and store it into
* request scope.
*
*
You may customize this method to allocate resources that are required
* for the lifetime of the current request.
*/
public void init() {
// Perform initializations inherited from our superclass
super.init();
// Perform application initialization that must complete
// *before* managed components are initialized
// TODO - add your own initialiation code here
//
// Initialize automatically managed components
// *Note* - this logic should NOT be modified
try {
_init();
} catch (Exception e) {
log("RequestBean1 Initialization Failure", e);
throw e instanceof FacesException ? (FacesException) e: new FacesException(e);
}
//
// Perform application initialization that must complete
// *after* managed components are initialized
// TODO - add your own initialization code here
}
/**
*
This method is called when this bean is removed from
* request scope. This occurs automatically when the corresponding
* HTTP response has been completed and sent to the client.
*
*
You may customize this method to clean up resources allocated
* during the execution of the init() method, or
* at any later time during the lifetime of the request.
*/
public void destroy() {
}
/**
*
Return a reference to the scoped data bean.
*/
protected SessionBean1 getSessionBean1() {
return (SessionBean1)getBean("SessionBean1");
}
/**
*
Return a reference to the scoped data bean.
*/
protected ApplicationBean1 getApplicationBean1() {
return (ApplicationBean1)getBean("ApplicationBean1");
}
}
/*
* SessionBean1.java
*
* Created on November 29, 2007, 9:30 AM
* Copyright kelly
*/
package fileuploadexample;
import com.sun.rave.web.ui.appbase.AbstractSessionBean;
import javax.faces.FacesException;
/**
*
Session scope data bean for your application. Create properties
* here to represent cached data that should be made available across
* multiple HTTP requests for an individual user.
*
*
An instance of this class will be created for you automatically,
* the first time your application evaluates a value binding expression
* or method binding expression that references a managed bean using
* this class.
*/
public class SessionBean1 extends AbstractSessionBean {
//
private int __placeholder;
/**
*
Automatically managed component initialization. WARNING:
* This method is automatically generated, so any user-specified code inserted
* here is subject to being replaced.
*/
private void _init() throws Exception {
}
//
/**
*
Construct a new session data bean instance.
*/
public SessionBean1() {
}
/**
*
This method is called when this bean is initially added to
* session scope. Typically, this occurs as a result of evaluating
* a value binding or method binding expression, which utilizes the
* managed bean facility to instantiate this bean and store it into
* session scope.
*
*
You may customize this method to initialize and cache data values
* or resources that are required for the lifetime of a particular
* user session.
*/
public void init() {
// Perform initializations inherited from our superclass
super.init();
// Perform application initialization that must complete
// *before* managed components are initialized
// TODO - add your own initialiation code here
//
// Initialize automatically managed components
// *Note* - this logic should NOT be modified
try {
_init();
} catch (Exception e) {
log("SessionBean1 Initialization Failure", e);
throw e instanceof FacesException ? (FacesException) e: new FacesException(e);
}
//
// Perform application initialization that must complete
// *after* managed components are initialized
// TODO - add your own initialization code here
}
/**
*
This method is called when the session containing it is about to be
* passivated. Typically, this occurs in a distributed servlet container
* when the session is about to be transferred to a different
* container instance, after which the activate() method
* will be called to indicate that the transfer is complete.
*
*
You may customize this method to release references to session data
* or resources that can not be serialized with the session itself.
*/
public void passivate() {
}
/**
*
This method is called when the session containing it was
* reactivated.
*
*
You may customize this method to reacquire references to session
* data or resources that could not be serialized with the
* session itself.
*/
public void activate() {
}
/**
*
This method is called when this bean is removed from
* session scope. Typically, this occurs as a result of
* the session timing out or being terminated by the application.
*
*
You may customize this method to clean up resources allocated
* during the execution of the init() method, or
* at any later time during the lifetime of the application.
*/
public void destroy() {
}
/**
*
Return a reference to the scoped data bean.
*/
protected ApplicationBean1 getApplicationBean1() {
return (ApplicationBean1)getBean("ApplicationBean1");
}
}
<?xml version="1.0" encoding="UTF-8"?>
Thanks for your response kelly.
snookcent@yahoo.com
jpecula@yahoo.com