Showing posts with label Struts. Show all posts
Showing posts with label Struts. Show all posts

30 August 2010

Using struct to represent bit fields

#include <stdio.h>

struct packed_data
{
unsigned int :3;
unsigned int f1:1;
unsigned int f2:1;
unsigned int f3:1;
unsigned int type:8;
unsigned int index:18;
};
int main(void)
{

struct packed_data pd;
pd.type = 257;
printf("%u\n", pd.type);

pd.f1 = 4;
printf("%u\n", pd.f1);

//printf("%p\n", &(pd.f1)); ERROR

return 0;
}

09 January 2009

Enabling Arabic in Java web applications (JSP apps and Struts apps )

Update here:
http://m-hewedy.blogspot.com/2010/05/enable-arabic-non-ascii-characters-for.html


Enabling Arabic (Unicode) in Java web applications (JSP apps and Struts apps )

One of the most big problems you can face as a java web developer is how
to make you web application accepts Arabic (non-ISO-8859-1 characters)
characters from the users in the input text boxes.

In JSP/Servlet applications, a small googling can solve the problem, but
with a framework such as Struts, the matter differ somewhat.

If your web application uses the Database ( the most if not all does )
you should insure that the problem is not in the Database itself that cannot
save Arabic characters, you should try hand-entering Arabic words in varchar
and nvarchar fields, if it accepts Arabic characters, then the problem is in
the JSP/Servlet or Struts and you will find the solution here.

1- To Enable Arabic for request parameteres of "POST" method:

For pure JSP/Servlet Applications :

Two steps and every thing well be done.

First: in each JSP page write this tag at the top of the page :

<%@page language="java" contentType="text/html; charset=UTF-8"%>

Second : in each Servlet that works as the controller for you JSPs, write these two statements at the top of your doPost() :

request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");

That’s all about Enabling Arabic character acceptation in your JSP forms

For Struts Applications:

Also two steps :
First: in each JSP page write this tag at the top of the page :

<%@page language="java" contentType="text/html; charset=UTF-8"%>

Second: write a Filter class the wraps your org.apache.struts.action.ActionServlet class and put these two statements in its doFilter() method

request.setCharacterEncoding("UTF-8");

response.setCharacterEncoding("UTF-8");

example filter :

public class ArabicEncodingFilter implements Filter {

private void doBeforeProcessing(ServletRequest request, ServletResponse response)

throws IOException, ServletException {

request.setCharacterEncoding("UTF-8");

response.setCharacterEncoding("UTF-8");

}

private void doAfterProcessing(ServletRequest
request, ServletResponse response)
throws IOException, ServletException {

}

public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)throws IOException, ServletException {

doBeforeProcessing(request, response);

Throwable problem = null;

try {

chain.doFilter(request, response);

} catch(Throwable t) {

problem = t;

t.printStackTrace();

}

doAfterProcessing(request, response);

}}}

And then wrap all your Servlets with you Filter by putting this in web.xml :

<filter>

<filter-name>EncodingFilter</filter-name>

<filter-class>ArabicEncodingFilter</filter-class>

</filter>

<filter-mapping>

<filter-name>EncodingFilter</filter-name>

<url-pattern>/*</url-pattern>

</filter-mapping>

2- Enable Arabic for request parameteres of "GET" method:

It depends on your server (tomcat/JBoss, etc), In tomcat 6 do the following:

set the URIEncoding attribute of the element in /conf/server.xml to UTF-8.

Or you can retrieve the request QueryString as is and URLDecode it using the desired encoding (UTF-8).

You can Wrtie a HttpServletRequestWrapper that wraps your HttpServletRequest's getParameter methods .

see :

http://balusc.blogspot.com/2009/05/unicode-how-to-get-characters-right.html
http://java.sun.com/developer/technicalArticles/Intl/HTTPCharset/
http://www.joelonsoftware.com/articles/Unicode.html
http://java.sun.com/javaee/5/docs/tutorial/doc/bnayb.html



for more, complete solustion, see : http://m-hewedy.blogspot.com/2010/05/enable-arabic-non-ascii-characters-for.html

13 December 2008

How to iterate over a collection using struts logic taglib

Among struts Tag libararies there's a taglib called logic, it aims to do logical operation especially values and strings comparison using its tags such as Equa, NotEqaul, present, notPresent, greaterThan and more.

Among theses tags, there is a very handy tag that used to iterate over collection, it remembers me with the JSTL forEach tag, but it provides more funcationality.

We can use this tag to display the model data as it appears in the next view in very easy-to-use way :

lets illustrate it, assume we have an abject called EmployeeBO that wrap and Employee object of four attributes : ssn, firstname, lastname and age.

The EmployeeBO object contains a method called getAllEmployees that returns an array list of employees objects.
The following code is the code of the action class, it inserts the result of "getAllEmployees" in the session to get it back again in the view from the session:

package org.daz;

import javax.servlet.http.*;
import java.util.*;
import org.apache.struts.action.*;

public class GetAllEmployeesAction extends org.apache.struts.action.Action {

public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {

ActionForward forward = null;

EmployeeBO emps = new EmployeeBO();
List empList = emps.getAllEmployees();

HttpSession session = request.getSession();
session.setAttribute("empList", empList);

forward = mapping.findForward("success");

return forward;
}
}


and this is the code of the jsp page:

<%@taglib uri="/WEB-INF/struts-html.tld" prefix="html"%>

<%@taglib uri="/WEB-INF/struts-logic.tld" prefix="logic"%>

<html:html>

<head>

<html:base />

</head>

<body>

<table border="1" width="50%">

<tr>

<td>serial</td>

<td>SSN</td>

<td>First name</td>

<td>Last name</td>

<td>Age</td>

</tr>

<logic:iterate id="employee"
name="empList" indexId="id" offset="${param.offset}" length="5">

<tr>

<td>${id }</td>

<td>${employee.ssn }</td>

<td>${employee.firstname }</td>

<td>${employee.lastname }</td>

<td>${employee.age }</td>

</tr>

</logic:iterate>

</table>

<html:link href="showemps.jsp?offset=${id - 9}"> previous </html:link>

<html:link href="showemps.jsp?offset=${id+1}"> next </html:link>

</body>

</html:html>

The logic tag appears in red, it takes more than one attribute;

id: an page-scoped object that will store the current iteration object

name : the name of the scopped bean to iterate over

indexId : an page-scoped object that will store the current iteration number

offset : the index of the collection to start from

length : the number of rows to display on a page