mardi 24 mars 2015

implementer JSP Servlet MVC code Project


Create a dynamic web project
For that go to
FileànewàDynamic web project
Fill project details and say next .next and finish and make sure generate web.xml is checked
Now time to create a servlet
For that right click on create project “SeverletJSPExapmle” ànewèservlet and give entries
Now in next we need to edit the URL mapping to make it more convenient because we are going to use this to access our servlet so I made them all small letter
Now say next and next and check the option  . Here we removed the dopost and doget and selected services. As they do same job
And say finish
Now eclipse has created  ServletExample.java file for us in source directory and added following entry in web.xml  (WebContentàWEB-INFàweb.xml)
<servlet>
    <description></description>
    <display-name>ServletExample</display-name>
    <servlet-name>ServletExample</servlet-name>
    <servlet-class>com.example.tutorial.ServletExample</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>ServletExample</servlet-name>
    <url-pattern>/servletexample</url-pattern>
  </servlet-mapping>
Now we will do some coding to interact our servlet with web browser
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            // it will get object to write string in browser
            PrintWriter out = response.getWriter();
            out.println("hello Java!!!");
      }
Now run this code by doing right click in ServletExample.Java file and select run AsàRun On Server
You will see your string in URL
You might need to restart once that tomcat server
Now time to some more advance programming by capturing request from JSP (xhtml) pages and sending response back
To create jsp page browse to webcontent folder  and select newà Jsp page
Now give JSP page name say index.jsp and click next and in next screen select  “New JSP file (xhtml)” as JSP template and say Finish
Now index.jsp page get created. To deploy the jsp page you need to right click on page and say new->run as-> on application sever
Here we will create a jsp page having a form with 2 input field firstname and lastname with a submit button
We will fill jsp form and capture that request in serverlet and display in web browser
We already created index.jsp page now time to add some code
<body>
<form action="servletexample" method="post" >
       <table border="0" >
             <tr>
                    <td>First Name: </td> <td><input type="text" name="firstname"/></td>
             </tr>
             <tr>
                    <td>Last Name: </td> <td><input type="text" name ="lastname"/></td>
             </tr>
             <tr>
                    <td colspan="2"><input type = "submit" value = "Submit"/></td>
             </tr>
       </table>
</form>
Here <form> to is to create a form
action= <give your servlet name (Not serverlet class name)> this is the place which setup binding between servlet and jsp
method = post means user will post below information as request from browse
<table> tag use to give good look and provide formatting
<tr> tag is to separate rows in table
<td> tag is for column
<td colspan="2"> means we are combining two column into one
Earlier we added very simple out put string in our ServletExample.java class here we are going to add some more code to capture JSP from request and display them in browser
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
             // Get servlet object which will display any string in browser
             PrintWriter out = response.getWriter();
             // Capture request from JSP page where input field name ="firstname"
             String fname = request.getParameter("firstname");
             // Capture request from JSP page where input field name ="lastname"
             String lname = request.getParameter("lastname");
             // Display string in web browser
             out.println(fname+" "+lname);
             //out.println("hello Java!!!");
            
       }
As JSP page should not be directly accessible to user so we will use  RequestDispatcher to hide our jsp page.
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//it will hide the jsp page link in URL and just show servlet URL it will use MVC model
//here we have add if condition so that jsp form will show if any input is null
 if(request.getParameter("firstname")==null || request.getParameter("lastname")==null ){
getServletContext().getRequestDispatcher("/index.jsp").forward(request, response);
}
// Capture request from JSP page where input field name ="firstname"
String fname = request.getParameter("firstname");
// Capture request from JSP page where input field name ="lastname"
String lname = request.getParameter("lastname");
// Display string in web browser
out.println(fname+" "+lname);
}
Now you can see index.jsp page from URL:
Now we will create one more jsp page output.jsp which will capture input entered in earlier created page  index.jsp . This page will display  the output in web. Again we will hind both JSP pages from user using   RequestDispatcher . Actually it will provide MVC model where web page will be display as per input . Like if any input first name or last name will be null then always index.jsp page will populate and once both entries will be there then output.jsp page will display first name and last name
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//it will hide the jsp page link in URL and just show servlet URL it will use MVC model
//here we have add if condition so that jsp form will show if any input is null
//we will create one more jsp page to show the output called output.jsp
if(request.getParameter("firstname")==null || request.getParameter("lastname")==null ){
getServletContext().getRequestDispatcher("/index.jsp").forward(request, response);
}
// Capture request from JSP page where input field name ="firstname"
String fname = request.getParameter("firstname");
// Capture request from JSP page where input field name ="lastname"
String lname = request.getParameter("lastname");
// Display string in web browser
out.println(fname+" "+lname);
//code to display output in output.jsp page
//Here we are setting attribute in request which is output.jsp page is fetching
request.setAttribute("firstname", fname);
request.setAttribute("lastname", lname);
//Get output.jsp page using request dispatcher
getServletContext().getRequestDispatcher("/output.jsp").forward(request, response);
}
Output.jsp:
<title>JSP Output</title>
</head>
<body>
      <h1>Your First and Last name is:</h1>
      <%
      String ffname= (String)request.getAttribute("firstname");
      String llname = (String)request.getAttribute("lastname");
      out.print(ffname+" "+llname);
      %>
</body>
<h1> tag we use for heading as it is in normal html
<% %> this tag use to write java scriptlet
Here you will notice we are getting both string from request.getAttribute which is set by servlet code. So servlet java code is working as mediocre which pop up and took input from index.jsp  after that set value to request object which is accessed by output.jsp and use in Java script and again servlet java code invoke the output.jsp
Now if you will say run as server from ServletExample.java then first index.jsp page will display but you will notice URL is :
You will give first name and last name and again output.jsp page URL will be : http://localhost:8081/ServerletsJSPExample/servletexample instead of http://localhost:8081/ServerletsJSPExample/output.jsp
It is because of request dispatcher you can see MVC model here.
The other way is capturing request in servlet by directly passing the value through URL like:
  
Complete working code:
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            // it will get object to write string in browser
            PrintWriter out = response.getWriter();
            if(request.getParameter("firstname") == null || request.getParameter("lastname")==null)
            {
                  getServletContext().getRequestDispatcher("/index.jsp").forward(request, response);
                  return;
            }
            String fname = request.getParameter("firstname");
            String lname = request.getParameter("lastname");
           
            request.setAttribute("firstname", fname);
            request.setAttribute( "lastname",lname);
           
            getServletContext().getRequestDispatcher("/output.jsp").forward(request, response);
           
            }
We can also use variable define in  java scriptlet in HTML tag to do more formatting and in our example we will print first name and lastname in HTML table in output.jsp using below code in body
<body>
      <h1>Your First and Last name is:</h1>
      <%
      String ffname= (String)request.getAttribute("firstname");
      String llname = (String)request.getAttribute("lastname");
      %>
     
      <table>
            <tr>
                  <td>First Name:</td><td><%=ffname %></td>
            </tr>
            <tr>
                  <td>Last Name:</td><td><%= llname%></td>
            </tr>
      </table>
</body>
Now the output will look like

mardi 17 février 2015

instalation tomcat : JAVA_HOME should point to a JDK not a JRE

Hello Every one, In day to day alignment some time we get some common errors and generally we spend a lot of time upon them. Recently i got an error , while doing setup of tomcat to run my flash+facebook application. To test my facebook application i was needed a web server that needs to be configure first . I used Tomcat 6.0. To make tomcat run applications smoothly we need to setup java_home or jre_home variables. Tomcat requires java to be install on your machine. as documented i put PATH and CLASSPATH variables according to the documentation like ….
variable: CLASSPATH
value : c:\Program Files\Java\jdk1.6.0\jre\lib;

variable: JAVA_HOME
value : C:\Program Files\Java\jdk1.6.0;
variable: PATH
value : C:\Program Files\Java\jre1.6.0;C:\Program Files\Java\jdk1.6.0\bin;C:\Program Files\Java\jre1.6.0\bin;C:\apache-tomcat-6.0.14\bin;
variable: JRE_HOME
value : C:\Program Files\Java\jre1.6.0;
but when i was running tomcat , the result was like this..
  • C:\apache-tomcat-6.0.14\bin>startup.bat   
  • The JAVA_HOME environment variable is not defined correctly   
    This environment variable is needed to run this program   
    NB: JAVA_HOME should point to a JDK not a JRE   
  • C:\apache-tomcat-6.0.14\bin>  
  • Solution : After search on google i found a solution, and the solution was to remove semi-colon from the end of JRE_HOME / JAVA_HOME variables. there should not be semi-colon…
    variable: JAVA_HOME
    value : C:\Program Files\Java\jdk1.6.0  (note : no semicolon at the end)

    variable: JRE_HOME
    value : C:\Program Files\Java\jre1.6.0  (note : no semicolon at the end)

    Apply above changes and run your tomcat successfully.
    Thanks.

    jeudi 26 juin 2014

    Group Items on Their Folder Name Inside a SharePoint Library


    As you probably will know, folders could not be used in views to group documents. You could only group documents on their metadata.
    Group Documents
    Group Documents
    A possible solution could be to add a new metadata field to the content type and manually add the folder name as the metadata value. It is very easy to add and requires no custom development, but it requires an extra manual step from the document creator.
    This manual step can also be automated by a SharePoint 2010 Document library functionality called Column default value settings. It can be found on the document library settings page under the General settings section.
    The Column default value settings enable you to define a default value for a metadata column per folder.
    Default column value
    Default column value

    Approach

    • Go to the document library settings;
    • Create a new column called: Folder name (Single line of text);
    • Click on Column default value settings under the General settings section;
      Culumn default value settings
      Culumn default value settings
    • On the left side you will see your folders, and on the right site the columns for which you could give up a default value;
      Folders, columns
      Folders, columns
    • Click on the folder name, and after that on the column name;
      Default column value
      Default column value
    • Click Use this default value, and fill in the folder value and click OK;
    • Repeat this process for each folder.
    Now when you upload a document to a folder, the Folder name column will automatically get the default value.
    To prevent users being able to fill in their own value, you could hide the Folder name columnfrom the new and edit forms.
    Hidde column from forms
    Hidde column from forms

    Result

    Group by on folder name
    Group by on folder name

    Attention: SharePoint Foundation

    The Column default value functionality is not available in SharePoint Foundation.

    Missing “Destination folder” on upload document form

    When uploading documents into a document library with folders, you normally see the following screen:

    Problem On one of our sites the Destination Folder was not available. After some investigating I found that the site where it didn’t work, was created using the “Blank site” template. 
    Solution Since we used the “Blank site” template, some features do not get activated by default.
    I found out that the feature that is responsible for the “Destination Folder” is the “Metadata Navigation and Filtering”feature.
    image
    After activating the solution, the Destination Folder became available. Note that if you turn this feature off, existing libraries will be able to select a destination folder, as where newly created document libraries will not be able to use this feature.