Pages

Showing posts with label servlets. Show all posts
Showing posts with label servlets. Show all posts

Thursday, 28 March 2013

Dispatch depending on the condition (JSP. Servlet)

The idea is easy: if condition is true - to show first JSP-page, otherwise - to show second JSP-page.
First of all i have index.jsp which makes a request to a servlet. Then that servlet uses class Client to keep all incoming information and transfers the instance of this Client class as a session's or request's attribute.

Displaying dynamically generated images in a JSP document

My small web application contains 1 java bean - helper class (Model), 2 JSP-pages (Views), 2 servlets (Controlers). First page index.jsp contains the html form to send a request with all requested information to a servlet.

Thursday, 21 March 2013

Servlet: HTML filtering

I took this instance from "Core Servlets and JavaServer Pages" book by Marty Hall and Larry Brown.
This is my text book at the moment.
Here is some code:
The main problem is html-tags.. If you try to print out "a<b" (a less than b) - you will not see any text after < symbol. But the method filter() can replace "<" symbol with "&lt;"  (predefined letters combination):



/** <b - as a html tag:
     if (a<b) {
        doThis();
     } else {
        doThat();
     }
     */
    public static String filter(String input) {
        StringBuilder filtered = new StringBuilder(input.length());
        char c;
        for(int i=0; i<input.length(); i++) {
            c = input.charAt(i);
            if (c == '<') {
                filtered.append("&lt;");
            } else if (c == '>') {
                filtered.append("&gt;");
            } else if (c == '"') {
                filtered.append("&quot;");
            } else if (c == '&') {
                filtered.append("&amp;");
            } else {
                filtered.append(c);
            }
        }
        return(filtered.toString());
    } 

Thursday, 7 March 2013

Servlet. Reading All Request Parameters

Using request.getParameterNames() to get all parameter names.
Using request.getParameterValues(paramName) to get all (multiple) parameter values.

Enumeration paramNames = request.getParameterNames(); //All parameters
while(paramNames.hasMoreElements()) {
                String paramName = (String)paramNames.nextElement();
                String[] paramValues = request.getParameterValues(paramName); //All values
....
}

If the paramValues array has only one entry and contains only one empty string, then the parameter had no values. If the array has more than one entry, then the parameter had multiple values.










An empty String is returned if the parameter exists but has no value, and null is returned if there was no such parameter which name you asked.

Wednesday, 6 March 2013

Servlet. Race condition.


Somewhere in html:

form action="SynchTest" method="POST" name="form1"
input name="SubmitBtn" type="submit" value="SUBMIT"

_______________
public class SynchTest extends HttpServlet {
        Asynch a = new Asynch();

    protected void processRequest(HttpServletRequest request, 
                                  HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();

        try {
            String title = "SynchTest";
            int y = 40;
            if(a.getValue() > y) {
                try {
                    Thread.sleep(5000);
                } catch (InterruptedException ex) {
                    out.print(ex.toString());
                }
                a.subtract(y);
            }
            String value = "Value is: " 
                            + String.valueOf(a.getValue()); 
            
            String threadInfo = Thread.currentThread().getName();
            value = value +"\n" + "Thread name is: " + threadInfo;
            out.println(MyHTML_util.getHTML(title,value));
        } finally {            
            out.close();
        }
    }
_____________________