Pages

Friday, June 25, 2010

<c:out = “” /> tag problem

While using the JSTL a very common problem faced is that

<c:out value="${now}"/> tags are not interpreted as they are supposed to be.

After going through the forums, I changed my xml version from 2.5 to 2.4 and all the tags started working correctly.

Earlier XML:

<?xml version="1.0" encoding="utf-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">

After Making the Changes:

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

 

References: http://forum.springsource.org/archive/index.php/t-21093.html

Thursday, June 24, 2010

Local Datastore Dashboard for Google App Engine

 

While the server is running… Here is the URL

http://localhost:8888/_ah/admin/datastore

Using the JSP Standard Tag Library (JSTL) in Spring Framework

We need jar files namely jstl.jar and standard.jar that are there in the Spring Framework, these files are to be copied to ‘/war/WEB-INF/lib' directory.

Also we have to copy this file from the Spring distribution ('spring-framework-2.5/dist/resources/spring-form.tld') to the 'springapp/war/WEB-INF/tld' directory that we also need to create. Next we must also add a <taglib/> entry to the 'web.xml' file.

  <jsp-config>
<taglib>
<taglib-uri>/spring</taglib-uri>
<taglib-location>/WEB-INF/tld/spring-form.tld</taglib-location>
</taglib>
</jsp-config>

Tuesday, June 22, 2010

Using Spring Framework and Google App Engine for a Web Application.

Step by Step Procedure for the Hello World Application:

  • Install the Google Plugin
  • Create a new Web application and untick GWT (Google Web Toolkit)
  • Download the latest Spring Release
  • Add the Jars to the folder WEB-INF/lib

The following JAR files should be present in the folder:

Untitled

Make the Following Changes to the web.xml file

<?xml version="1.0" encoding="utf-8"?>

<web-app xmlns="http://java.sun.com/xml/ns/javaee" version="2.5">
    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/a/*</url-pattern>
    </servlet-mapping>
</web-app>

Create a file dispatcher-servler.xml in the WEB-INF Directory

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"
>

<context:component-scan base-package="ipathshala.controllers" />

<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/views/" p:suffix=".jsp" >
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
</bean>

</beans>



HelloController.java

package ipathshala.controllers;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;

@Controller
@RequestMapping("/hello")
public class HelloController {

private UserService userService = UserServiceFactory.getUserService();

@RequestMapping(method = RequestMethod.GET)
public String hello(HttpServletRequest request, ModelMap model)
{
String url = request.getRequestURI();
String message;

if(request.getUserPrincipal() != null)
{
message = new StringBuilder()
.append("Hello, ")
.append(request.getUserPrincipal().getName())
.append("! You can <a href=\"")
.append(userService.createLogoutURL(url))
.append("\">Sign Out</a>.").toString();
}else{
message = new StringBuilder()
.append("Please ")
.append("<a href=\"")
.append(userService.createLoginURL(url))
.append("\">Sign In</a>.").toString();
}

model.addAttribute("message", message);

return "hello/hello";
}

}
This line above @RequestMapping("/hello") maps all the requests /a/hello to this controller after being examined by the DispatcherServlet.

Create a Folder views under WEB-INF and two Sub folders hello and common.


hello/hello.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page isELIgnored="false" %>
<%@ include file="/WEB-INF/views/common/includes.jsp" %>
<p>${message}</p>



common/includes.jsp

 
<%@ page session="false"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>





You should now be able to click “Debug” and run the application by visiting “localhost:8888/a/hello”.


References:



 

 

Friday, June 11, 2010

Session Management using Google User on Google App Engine

Code to be inserted in every JSP Page


HttpSession s = request.getSession();
if (s.getAttribute("logged")==null){
s.setAttribute("from",request.getRequestURI());
response.sendRedirect("/login");
}
else {
s.setAttribute("from",request.getRequestURI());
%>Logout<%
//UserService userSer = (UserService)s.getAttribute("userSer");
}



Login.java


public class Login extends HttpServlet {
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws IOException {
     UserService userSer=UserServiceFactory.getUserService();
     User u=userSer.getCurrentUser();
     HttpSession s=request.getSession();
     String from=(String)s.getAttribute("from");
     s.setAttribute("logged","yes");
     s.setAttribute("userSer", userSer);
     response.sendRedirect(userSer.createLoginURL(from));
    
    }
}

Logout.java


public class Logout extends HttpServlet {
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws IOException {
    
     HttpSession s = request.getSession();
     String from=(String)s.getAttribute("from");
     UserService us=(UserService)s.getAttribute("userSer");
     s.invalidate();
     response.sendRedirect(us.createLogoutURL("/index.html"));
    
    }
}

Java Best Practices

A good article on Java Best Practices

http://java.sun.com/developer/technicalArticles/javaserverpages/servlets_jsp/

Monday, June 7, 2010

Edit Form Example using php

$query = mysql_query("SELECT * FROM table") or die(mysql_error());
while ($row = mysql_fetch_assoc($query))
{
eco '';
}
Read eco as echo

Tuesday, June 1, 2010

A better and Efficient Date input Calendar

The previous blog post about Input Date Calendar mentioned a link to the project hosting of a date input calendar but it seems like that its not good enough. So I found this another link, This one is more efficient and better and easy to use.

http://www.dynamicdrive.com/dynamicindex7/jasoncalendar.htm