JavaTechie

Its all about Technology

Sending mail with attachment with javamail December 26, 2007

Filed under: Java — javatechie @ 5:30 am

///     /// Send Message     ///     String to = args[0];     String from = args[1];     String host = args[2];     boolean debug = Boolean.valueOf(args[3]).booleanValue();

     // create some properties and get the default Session     Properties props = new Properties();     props.put("mail.smtp.host", host);

     Session session = Session.getDefaultInstance(props, null);     session.setDebug(debug);

     try {         // create a message         MimeMessage msg = new MimeMessage(session);         msg.setFrom(new InternetAddress(from));         InternetAddress[] address = {new InternetAddress(to)};         msg.setRecipients(Message.RecipientType.TO, address);         msg.setSubject("JavaMail APIs Multipart Test");         msg.setSentDate(new Date());

         // create and fill the first message part         MimeBodyPart mbp1 = new MimeBodyPart();         mbp1.setText("Your mail");

         // create and fill the second message part         MimeBodyPart mbp2 = new MimeBodyPart();         // Use setText(text, charset), to show it off !         mbp2.setContent(msgText2, "image/jpg");

         // create the Multipart and its parts to it         Multipart mp = new MimeMultipart();         mp.addBodyPart(mbp1);         mp.addBodyPart(mbp2);

         // add the Multipart to the message         msg.setContent(mp);

         // send the message         Transport.send(msg);     } catch (MessagingException mex) {         mex.printStackTrace();         Exception ex = null;         if ((ex = mex.getNextException()) != null) {        ex.printStackTrace();         }     }

 

What is Tomcat? December 26, 2007

Filed under: Uncategorized — javatechie @ 4:41 am
Tags:

“Tomcat is a free, open-source implementation of Java Servlet and JavaServer Pages technologies developed under the Jakarta project at the Apache Software Foundation. Tomcat is available for commercial use under the ASF license from the Apache web site in both binary and source versions.”

 

Pagination December 26, 2007

Filed under: Java — javatechie @ 4:26 am

Often a database query will yield hundreds or thousands of rows of data. Trying to display all of them in one webpage could be very slow and resource intensive. Worse, it results in a poor user experience for the person who actually has to scroll through all of this data.

Well-designed websites will return a small subset of the data and provide links to move to the next or last set.

The easiest way to accomplish this is by constraining the number of rows returned from the database query in the first place.

  • MySQL uses the ‘LIMIT’ keyword for this.
  • PostgreSQL uses the ‘LIMIT’ and ‘OFFSET’ keywords for this.
  • Oracle uses the ‘ROWNUM’ keyword. It doesn’t work like LIMIT and OFFSET. You will probably find it strange.
  • DB2 uses the ‘ROWNUMBER’ ‘OVER’ combination.
  • SQL Server uses the ‘TOP’ keyword, followed by the number of rows from the top of the result.

A typical query in MySQL would look something like:

SELECT * FROM my_table LIMIT 10, 50

This will yield rows 51 through 60.


Another way to limit the number of records returned is to use the
java.sql.Statement.setMaxRows(int)

Because this is part of the JDBC API, it works for all servers.

It should be noted that with both approaches it is undefined which rows are returned. If you want particular rows returned, you should add an “order …” clause so that the order of rows is defined.

 

how to find carriage return in string using java December 13, 2007

Filed under: Java — javatechie @ 9:36 am

Just like you would identify any other character in a string: look for “\r\n” or “\n” (system dependent).

 

<html:errors> in struts December 13, 2007

Filed under: Java — javatechie @ 6:03 am
Tags:
errors.header=<UL>errors.footer=</UL>
errors.prefix=<LI>errors.suffix=</LI>

The default code for queuing an error message is:

errors.add(ActionErrors.GLOBAL_ERROR,new ActionError("error.username.required"));

To specify that this message is for the “username” property, we would code this instead:

errors.add("username", new ActionError("error.username.required"));

<html:errors property="name_of_the_field"/>
 

Working with JSP Sessions December 13, 2007

Filed under: Java — javatechie @ 5:57 am

* getCreationTime —returns value of time in milliseconds.
* getLastAccessedTime—returns the value of time in milliseconds.
* getId—method of session object is used to return the unique identifier associated with the session.
* invalidate()
* getMaxInactiveInterval
* setMaxInactiveInterval()
* removeAttribute(String name)
* setAttribute(String, object)

 

Checking the results of a ResultSet December 13, 2007

Filed under: Java — javatechie @ 5:21 am

boolean recordsWereFound = false;
while (rs.next()) {
recordsWereFound = true;
// process this record;
}
if (! recordsWereFound) {
// do this if no records were found
}

 

forward from one application to other application December 13, 2007

Filed under: Java — javatechie @ 5:20 am

The number of ways in which we can forward from one application to another application running on different containers.I think these 2 can work :
1.ServletContext.getRequestDispatcher.forward(“URL of the 2nd application”)
2.response.sendRedirect(“URL”)

 

Pagination in JSP/Java December 13, 2007

Filed under: Java — javatechie @ 4:58 am

The situation is first count how many rows in the database, then query the database by page size (how many rows you want to display on a web page).

In Jsp

String pno=request.getParameter(“pno”); // this will be coming from url
int pageno=0;
if(pno==null)
{
pageno=1;
}
else
{
pageno=Integer.parseInt(pno);
}
int next=pageno+1;
int previous=pageno-1;

if(previous!=0)
{
Previous link (url.jsp?pno=previous)

}

int max=Total Rows; // total number of rows from database table

int check=(next-1)*10; // each page 10 rows

if(check<max) // print next link if check is less than total rows
{
Next link (url.jsp?pno=next)
}

In Java

int offset = (pagenumber – 1) * 10;

Query + limit offset ,10;


 

Using POST method in XMLHTTPRequest(Ajax) December 12, 2007

Filed under: Uncategorized — javatechie @ 11:26 am
Tags:

Requirements

Create a XMLHTTPRequest Object that uses the POST method.

var http = new XMLHttpRequest();

Using GET method

Now we open a connection using the GET method.

var url = "get_data.php";
var params = "lorem=ipsum&name=binny";
http.open("GET“, url+”?”+params, true);
http.onreadystatechange = function() {
//Call a function when the state changes.
if(http.readyState == 4 && http.status == 200) {
alert(http.responseText);}}
http.send(null);

I really hope that this much is clear for you

POST method

We are going to make some modifications so POST method will be used when sending the request…

var url = "get_data.php";
var params = "lorem=ipsum&name=binny";
http.open("POST“, url, true);

//Send the proper header information along with the request
http.setRequestHeader(”Content-type”, “application/x-www-form-urlencoded”);
http.setRequestHeader(”Content-length”, params.length);
http.setRequestHeader(”Connection”, “close”);

http.onreadystatechange = function() {
//Call a function when the state changes.
if(http.readyState == 4 && http.status == 200) {
alert(http.responseText);}}
http.send(params);

The first change(and the most obvious one) is that I changed the first argument of the open function from GET to POST. Also notice the difference in the second argument – in the GET method, we send the parameters along with the url separated by a ‘?’ character…

http.open("GET",url+”?”+params, true);

But in the POST method we will use just the url as the second argument. We will send the parameters later.

http.open("POST", url, true);

Some http headers must be set along with any POST request. So we set them in these lines…

http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
http.setRequestHeader("Content-length", params.length);
http.setRequestHeader("Connection", "close");

With the above lines we are basically saying that the data send is in the format of a form submission. We also give the length of the parameters we are sending.

http.onreadystatechange = function() {
//Call a function when the state changes.
if(http.readyState == 4 && http.status == 200) {
alert(http.responseText);}}

We set a handler for the ‘ready state’ change event. This is the same handler we used for the GET method. You can use the http.responseText here – insert into a div using innerHTML(AHAH), eval it(JSON) or anything else.

http.send(params);