Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
J2ME Servlet Example 
 

This is the simple servlet tutorial. In this tutorial we shows you, how to create the servlet and implement it with the midlet.

 

J2ME Servlet Example

                         

This is the simple servlet tutorial. In this tutorial we shows you, how to create the servlet and implement it with the midlet. In this servlet we make connection with the database and find the cookies value. To create the servlet we follow the list of  points given below:

  1. Create your own folder in the C:\apache-tomcat-6.0.16\webapps location.
  2. Place WEB-INF folder in your own created folder.
  3. Create classes, lib and web.xml file in the WEB-INF folder.
  4. Create your Servlet in the classes folder.
  5. Save it with .java extension.
  6. Mapping your servlet in the web.xml file.
  7. Compile the java file on the command window with the specific path.
  8. Run the tomcat server from startup.bat file.
  9. Run the localhost.
  10. Give the servlet path in the address bar of your browser.

For your help we provide to you the source code of the servlet and web.xml, you simply download the source code and follows the above steps.

For Details follow this link: J2ME Cookies Example

 

J2MEServletExample.java

import java.io.*;
import java.sql.*;
import java.text.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class J2MEServletExample extends HttpServlet{
  String url = "jdbc:mysql://localhost:3306/";
  String dbName = "test";
  String driverName = "com.mysql.jdbc.Driver";
  String userName = "root";
  String password = "root";
  Connection con = null;
    
  protected void doGet(HttpServletRequest req, HttpServletResponse res
    throws 
ServletException, IOException{
    Cookie[] cookies = req.getCookies();
    System.out.println("cookies "+cookies);
    res.setContentType("text/html");    
    PrintWriter out = res.getWriter();
    out.println("<html>");
    out.println("<head><title>J2ME Servlet</title></head>");
    out.println("<body>");
    out.println("<h1>J2ME Servlet</h1>");
    out.println("</body></html>");  
    
    if(cookies != null){
      Cookie theCookie = cookies[0];
      String id = theCookie.getValue();
      out.println("<b>The Cookies Value is:</b> " + id);
      out.print("<br>");
      out.println("And The Table Data is Display Here<br>");
    else {
      int random = (intMath.round(100 * Math.random());
      Cookie cookie = new Cookie("ID", Integer.toString(random));
      System.out.println("cookie "+cookie);
            res.addCookie(cookie);
    }

    try{
      Class.forName(driverName).newInstance()
      con = DriverManager.getConnection(url+dbName, userName, password);
      Statement stmt = con.createStatement();
      java.util.Date currentTime = new java.util.Date()
      SimpleDateFormat formatter = new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss");
      String dateString = formatter.format(currentTime);
      String query = "UPDATE emp set lastAccess = '" + dateString + "' where id = 2";
      stmt.executeUpdate(query);

      ResultSet rs = stmt.executeQuery("Select * from emp");       
      while(rs.next()){
        out.println(rs.getString(1"     " + rs.getString(2"     " 
        rs.getString
(3));
        out.println("<br>");
      }
    }catch (Exception e){
      System.out.println(e);
    }  
    out.close();
  }
}

 

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_9" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">


<display-name>Servlet 2.4 application</display-name>

<servlet>
<servlet-name>J2MEServletExample</servlet-name>
<servlet-class>J2MEServletExample</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>J2MEServletExample</servlet-name>
<url-pattern>/J2MEServletExample</url-pattern>
</servlet-mapping>

</web-app>

Download This Source Code

                         

» View all related tutorials
Related Tags: c j2me diff types type if example to rectangle draw exam raw angle e il pe in different m nt

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Training Courses
Tell A Friend
Your Friend Name
Software Solutions
Least Viewed
Most Rated
Recently Viewed
Search Tutorials

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.