Posts

Showing posts with the label JSP Servlet

Sample JSP Date Combo

This method produces required html code for date time combos. Date parameter is selected in case of equality. public static String printDateCombos(String name , java.util.Date date) { Calendar today = Calendar.getInstance(new Locale("en-US")) ; if (date == null) today.setTimeInMillis( System.currentTimeMillis() ); else today.setTime( date ) ; int year = today.get( Calendar.YEAR ); int month = today.get( Calendar.MONTH ); int day = today.get( Calendar.DAY_OF_MONTH ); int hour = today.get( Calendar.HOUR_OF_DAY ); int min = today.get( Calendar.MINUTE ); StringBuffer out = new StringBuffer(); out.append("<select name=\""); out.append(name); out.append("_day\">"); for (int i=1;i out.append("<option"); if (i==day) out.append(" selected "); out.append(">"); if (i out.append(i); out.append("</option>"); } out.append("</selec...

JSP Sessions Example

Sessions are started automatically in JSP pages. You can access current session by prebuilt session object. Create sessions.jsp file on your Tomcat's webapps/ROOT directory : out.print("Session Creation Time:" + new Date(session.getCreationTime())); out.print("<BR>"); out.print("Last accessed Time:" + new Date(session.getLastAccessedTime())); out.print("<BR>"); out.print("Session ID:"+session.getId()); %> and launch it (for example http://localhost:8080/sessions.jsp) it prints: Session Creation Time:Sat Jun 14 19:28:06 EEST 2008 Last accessed Time:Sat Jun 14 19:33:34 EEST 2008 Session ID:365A3F239D1E54FD43EA0F7CBA1931EF Now we are able to access the session. We can store objects in session objects. This can help share objects between JSP pages. Sessions are widely used for authentication purposes. For example , let this be index.jsp as default page : <% String userName = request.getParameter(...

Simple JSP Tutorial

This jsp accepts a number from an html form and prints squareroot of that number on the same page. Paste this code to file "first.jsp" and move it to tomcat's webapps/ROOT directory. Start tomcat and browse to localhost:8080/first.jsp. We put java code in block , other parts are standart html. prints variable into html code just like does. request , response and out objects are standart JSP objects that can be used to access http request and response. <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Sample JSP</title> </head> <body> <% String strNumber= request.getParameter("number"); double...

Escape HTML and XML in Java

HTML and XML escape format is widely used in web and XML transactions. There is a great utility for this purpose in Java world : Lang package in apache commons project Download commons-lang and put the commons-lang-xxx.jar file in your classpath or in your project. The StringEscapeUtils class is your key class here. Use static escapeXml() , escapeHtml() , unescapeXml() , unescapeHtml() methods. String escaped = StringEscapeUtils.escapeXml(" "); Great & Lifesaving , thanks apache again !