Monday, 5 November 2012

J2ee Tutorial Part 1 -> Sample web application

//creating a sample web application using eclipse and tomcat server

Step 1 :-Download Eclipse  and Apache tomcat 7.0
Step 2 :-Run Eclipse and add Tomcat as a server to Eclipse
Step 3 :-Go to file new and create a new dynamic web project


Step 4:- Go to web.xml in the WEB-INF project and change it as follow

<?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" id="WebApp_ID" version="2.5">
  <display-name>rakesh</display-name>
  <welcome-file-list>
    <welcome-file>login.jsp</welcome-file> //this jsp page will be loaded first
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  <servlet>
    <description></description>
    <display-name>loginServlet</display-name>
    <servlet-name>loginServlet</servlet-name>//this will be servlet name
    <servlet-class>com.tcs.practice.servlets.loginServlet</servlet-class>//this is our servlet
  </servlet>
  <servlet-mapping>
    <servlet-name>loginServlet</servlet-name>
    <url-pattern>/loginServlet</url-pattern>//url mapping so that using this we can invoke the servlet
  </servlet-mapping>
</web-app>

Step 5:- Create a jsp with name login under the webcontent folder this will be the first page to be loaded

paste the bellow content in the jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<h1 align="center">login</h1>
<body>
<form action="<%=request.getContextPath()%>/loginServlet" method="post">
<fieldset>
User name: <input type="text" name="UserName" /><br />
<br />
Password : <input type="password" name="Password" /><br><br>
<input type="submit" value="Submit" />
<a href="reg.jsp">register</a>
</fieldset>
</form>
</body>
</html>

               the above is a jsp which will ask for login information if the login details are correct i will go to success.jsp if the details are incorrect it will go to error .jsp

Step 6:- Create two jsps with one Success.jsp with bellow content
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h1>welcome to home page</h1>
</body>
</html>
Step 7:- Create  error .jsp with bellow content
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h1>error</h1>
</body>
</html>

Step 8:- Go to src folder and create a new package com.tcs.practice.servlets and right click on the package and create a new servlet with name LoginServlet and paste the bellow content bellow


package com.tcs.practice.servlets;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.tcs.practice.dao.CheckLogin;
import com.tcs.practice.dto.LoginBean;

/**
 * Servlet implementation class loginServlet
 */
public class loginServlet extends HttpServlet {//for a class to be a servlet it should extend generic servlet
    private static final long serialVersionUID = 1L;
     
    /**
     * @see HttpServlet#HttpServlet()
     */
    public loginServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
      
        String username=request.getParameter("UserName");//getting username from request
      
        System.out.println("this is username"+username);
        String password=request.getParameter("Password");//getting password from request
        System.out.println("this is password"+password);
        
        if(username.equals("admin")&&passsword.equals("admin"))
        response.sendRedirect("success.jsp"); //if matches the success.jsp will be loaded
        else
        response.sendRedirect("error.jsp");//if error the error .jsp will be loaded
          
    }

}

Step 9:-Rightclick on project clean build the project and then run the project
Step 10:-End of tutorial part one.

Sunday, 4 November 2012

Configuring iwidget in Ibm Connections 3.0

 //Configuring iwidget in Ibm Connections 3.0

1) Start the admin client
Go to \IBM\WebSphere\AppServer\profiles\<dmgr_profile>\bin>
wsadmin -lang jython -user <user> -password <password> -port 8879

2) Start the Communities Admin service
execfile("communitiesAdmin.py")

3) Check-out the widgets-config.xml
CommunitiesConfigService.checkOutWidgetsConfig(<temp dir>,<cell_name>)
Ex: CommunitiesConfigService.checkOutWidgetsConfig("c:/temp", AdminControl.getCell())


4) Make the changes to the widgets-config.xml file

<resource type="community" …>
…
<!-- yourWidget_name -->
  <widgetDef defId="yourWidget_name" primaryWidget="false" modes="view edit" showInPalette="true" uniqueInstance="true"
                url="{contextRoot}/projectName/projectName.xml?version={version}" loginRequired="true"
                description="Pep Subject Matter Expert widget.">
</widgetDef>
…
</resource>
5) Check-in the widgets-config.xml file
CommunitiesConfigService.checkInWidgetsConfig(<temp dir>,<cell_name>)

6) Restart the Communities application in WAS

Using Abdera Client creating an entry in the blog of Ibm Connections3.0

//Creating an entry in the blog


try{//surround with try catch
             Abdera abdera = new Abdera();
             String new_Title = "sample_entry";
              AbderaClient client = new AbderaClient(abdera);
              client.addCredentials(AuthScope.ANY_HOST, AuthScope.ANY_REALM,
                        AuthScope.ANY_SCHEME, new UsernamePasswordCredentials(StringConstants.USERNAME,StringConstants.PASSWORD));//passing credentials username/password
                ClientResponse resp = client.get("https://x.y..com/blogs/"+communityID+"/api/entries");
                 Entry entry = abdera.newEntry();//creating a new entry
                        entry.addAuthor("xyz");
                        entry.setTitle(new_Title);
                        entry.addCategory("partner");
                        entry.addCategory("demo");
                        entry.setContentAsHtml(reviewContent);
                        resp = client.post(resp.getUri(), entry);//posting the entry
                        statusCode = resp.getStatus();
                        System.out.println("Status -->: "+resp.getStatus());//printing the status code

        }catch(Exception e){
            e.printStackTrace();
        }

Using Abdera Client Creating Community in Ibm Connections 3,0

//Creating community pragmatically in Ibm Connections3.0 using abdera client


String communtiy_name="divine";//mention the name of the community
 Abdera abdera = new Abdera();
 AbderaClient client = new AbderaClient(abdera); //creating abdera client object
 try {//surround it with try and catch
 client.addCredentials(AuthScope.ANY_HOST, AuthScope.ANY_REALM,
 AuthScope.ANY_SCHEME, new UsernamePasswordCredentials("xxxx", "yyyy"));//pass your username and password
 System.out.println("After adding credentials");
 ClientResponse resp = client.get("&nbsp;&nbsp;&nbsp;" title="https://x.y..com/communities/service/atom/communities/my");//title will be the post url
System.out.println("URI "+resp.getUri());
 Entry entry = abdera.newEntry();//creating an entry
 entry.setTitle(communtiy_name);//setting community title

 entry.setContentAsHtml("This is a practicse community");
 entry.addCategory("http://www.ibm.com/xmlns/prod/sn/type", (http://www.ibm.com/xmlns/prod/sn/type%22,) "community", "community");//specifying that it is a community
 System.out.println("XML as string is: "+entry.toString());//checking our entry
 resp = client.post(resp.getUri(), entry);//performing the post operation by passing the entry
 System.out.println("Status -->: "+resp.getStatusText()); //checking the status
 System.out.println("Response document is: "+resp.getDocument()); //getting the document if needed you can parse it using abdera you can see my next post for parsing using abdera

 }
 catch(Exception e)
 {
 e.printStackTrace();
 }
 

Custom single threaded java server

 package com.diffengine.csv; import java.io.*; import java.net.*; import java.util.Date; public class Server { public static void main(Str...