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:
- Create your own folder in the C:\apache-tomcat-6.0.16\webapps location.
- Place WEB-INF folder in your own created folder.
- Create classes, lib and web.xml file in the WEB-INF folder.
- Create your Servlet in the classes folder.
- Save it with .java extension.
- Mapping your servlet in the web.xml file.
- Compile the java file on the command window with the specific path.
- Run the tomcat server from startup.bat file.
- Run the localhost.
- 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 = (int) Math.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>