Total Pageviews

Wednesday 4 April 2012

best way to keep data in the form when failing to submit in JSP

Problem:
When submiting the page, sometime it fails because of the validation. At this time, the data in the page  are lost, and we have to type everything again.
Solution:
Declare the varaible at the beginning, and assign the value from the request. But if it's first fime calling the page, assign it as empty string.

        <%
        String xmlStr=request.getParameter("xmlStr");if(xmlStr==null)xmlStr="";
        %>
         <form method=GET >
                <tr>
                    <td><textarea name="xmlStr" rows="12" cols="100"><%=xmlStr%></textarea></td>                   
                </tr>              
               <tr><td><input type=submit name=submit value='Post'/></td></tr>
            </table>           
        </form>
        <%
         if(request.getParameter("submit") != null){
          
                  if(xmlStr == null || xmlStr.equals("")) {
                  out.println("<p>xmlStr empty!</p>");  
                 return;
           }         
           // do whatever
         }
         %>
 

how to handle binary data such as image in soap based web service

Web Service Part:
supposed that  you have a picture named Desert.jpg in package resources

@WebService(serviceName = "PictureService")
@Stateless()
public class PictureService {   
      
     @WebMethod(operationName = "getImage")
    public byte[] getImage( ) throws IOException {
   URL resource = this.getClass().getResource("/resources/Desert.jpg");
   InputStream in = resource.openStream();
     ByteArrayOutputStream bos = new ByteArrayOutputStream();
     byte[] buf = new byte[1024];
    for(int read; (read = in.read(buf)) != -1;) {
        bos.write(buf, 0, read);
     }
        return bos.toByteArray()s;
    }
   
}

Client part as Servlet:

public class test extends HttpServlet {
   
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
      
        try {
            PictureService_Service service = new PictureService_Service();
            PictureService port=service.getPictureServicePort(); 
           
            response.setContentType("image/jpeg");  
            OutputStream out = response.getOutputStream();  
           
            byte[] content = port.getImage();
             
            out.write(content);  
           
            out.close();
        } catch (IOException_Exception ex) {
            Logger.getLogger(test.class.getName()).log(Level.SEVERE, null, ex);
        }
            }
   
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }
   
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }
   
    @Override
    public String getServletInfo() {
        return "Short description";
    }
}

Sunday 1 April 2012

Unmarshalling from XML file and marshalling to XML file

            ObjectFactory of = new ObjectFactory();
            FlightInfo sh = of.createFlightInfo();
            //Unmarshalling
            JAXBContext ctx = JAXBContext.newInstance(sh.getClass().getPackage().getName());
            Unmarshaller unm = ctx.createUnmarshaller();
            sh= (FlightInfo)unm.unmarshal(new File("flight.xml"));

           //do something with object sh

            //Marshalling
            try {               
                javax.xml.bind.Marshaller marshaller = ctx.createMarshaller();
                marshaller.setProperty(javax.xml.bind.Marshaller.JAXB_ENCODING, "UTF-8");
                marshaller.setProperty(javax.xml.bind.Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
                marshaller.marshal(sh, new FileOutputStream(new File("flight.xml")));
            } catch (javax.xml.bind.JAXBException ex) {
            // Handle exception
            java.util.logging.Logger.getLogger("global").log(java.util.logging.Level.SEVERE, null, ex);
            }    

How to convert String to XMLGregorianCalendar

String mydatetime="2011-09-29T08:55:00";
XMLGregorianCalendar xgc=DatatypeFactory.newInstance().newXMLGregorianCalendar(mydatetime);