Tuesday, 16 January 2018

HttpSession with example in Servlet


HttpSession with example in Servlet


The HttpSession object is used for session management. A session contains information specific to a particular user across the whole application. When a user enters into a website (or an online application) for the first time HttpSession is obtained via request.getSession(), the user is given a unique ID to identify his session. This unique ID can be stored into a cookie or in a request parameter.
The HttpSession stays alive until it has not been used for more than the timeout value specified in tag in deployment descriptor file( web.xml). The default timeout value is 30 minutes, this is used if you don’t specify the value in tag. This means that when the user doesn’t visit web application time specified, the session is destroyed by servlet container. The subsequent request will not be served from this session anymore, the servlet container will create a new session.
This is how you create a HttpSession object.
protected void doPost(HttpServletRequest req,
    HttpServletResponse res)
    throws ServletException, IOException {
        HttpSession session = req.getSession();
}
You can store the user information into the session object by using setAttribute() method and later when needed this information can be fetched from the session. This is how you store info in session. Here we are storing username, emailid and userage in session with the attribute name uName, uemailId and uAge respectively.
session.setAttribute("uName", "ChaitanyaSingh");
session.setAttribute("uemailId", "myemailid@gmail.com");
session.setAttribute("uAge", "30");
This First parameter is the attribute name and second is the attribute value. For e.g. uName is the attribute name and ChaitanyaSingh is the attribute value in the code above.
TO get the value from session we use the getAttribute() method of HttpSession interface. Here we are fetching the attribute values using attribute names.
String userName = (String) session.getAttribute("uName");
String userEmailId = (String) session.getAttribute("uemailId");
String userAge = (String) session.getAttribute("uAge");

Methods of HttpSession

public void setAttribute(String name, Object value): Binds the object with a name and stores the name/value pair as an attribute of the HttpSession object. If an attribute already exists, then this method replaces the existing attributes.
public Object getAttribute(String name): Returns the String object specified in the parameter, from the session object. If no object is found for the specified attribute, then the getAttribute() method returns null.
public Enumeration getAttributeNames(): Returns an Enumeration that contains the name of all the objects that are bound as attributes to the session object.
public void removeAttribute(String name): Removes the given attribute from session.
setMaxInactiveInterval(int interval): Sets the session inactivity time in seconds. This is the time in seconds that specifies how long a sessions remains active since last request received from client.
For the complete list of methods, refer the official documentation.

Session Example

index.html
<form action="login">
  User Name:<input type="text" name="userName"/><br/>
  Password:<input type="password" name="userPassword"/><br/>
  <input type="submit" value="submit"/>
</form>
MyServlet1.java
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class MyServlet1 extends HttpServlet {
   public void doGet(HttpServletRequest request, HttpServletResponse response){
     try{
      response.setContentType("text/html");
      PrintWriter pwriter = response.getWriter();

      String name = request.getParameter("userName");
      String password = request.getParameter("userPassword");
      pwriter.print("Hello "+name);
      pwriter.print("Your Password is: "+password);
      HttpSession session=request.getSession();
      session.setAttribute("uname",name);
      session.setAttribute("upass",password);
      pwriter.print("<a href='welcome'>view details</a>");
      pwriter.close();
    }catch(Exception exp){
       System.out.println(exp);
     }
  }
}
MyServlet2.java
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class MyServlet2 extends HttpServlet {
  public void doGet(HttpServletRequest request, HttpServletResponse response){
  try{
      response.setContentType("text/html");
      PrintWriter pwriter = response.getWriter();
      HttpSession session=request.getSession(false);
      String myName=(String)session.getAttribute("uname");
      String myPass=(String)session.getAttribute("upass");
      pwriter.print("Name: "+myName+" Pass: "+myPass);
      pwriter.close();
  }catch(Exception exp){
      System.out.println(exp);
   }
  }
}
web.xml
<web-app>
<servlet>
   <servlet-name>Servlet1</servlet-name>
   <servlet-class>MyServlet1</servlet-class>
</servlet>
<servlet-mapping>
   <servlet-name>Servlet1</servlet-name>
   <url-pattern>/login</url-pattern>
</servlet-mapping>
<servlet>
   <servlet-name>Servlet2</servlet-name>
   <servlet-class>MyServlet2</servlet-class>
</servlet>
<servlet-mapping>
   <servlet-name>Servlet2</servlet-name>
   <url-pattern>/welcome</url-pattern>
</servlet-mapping>
</web-app>
Output:
First Screen:
After clicking Submit:
After clicking view details:

Monday, 8 January 2018

Git - Remove untracked files

Remove untracked files from the working tree

Step 1 is to show what will be deleted by using the -n option:
git clean -n
Clean Step - beware: this will delete files:
git clean -f
  • To remove directories, run git clean -f -d or git clean -fd
  • To remove ignored files, run git clean -f -X or git clean -fX
  • To remove ignored and non-ignored files, run git clean -f -x or git clean -fx
Note the case difference on the X for the two latter commands.
If clean.requireForce is set to "true" (the default) in your configuration, one needs to specify -fotherwise nothing will actually happen.
Again see the git-clean docs for more information.

Options

-f
--force
If the Git configuration variable clean.requireForce is not set to false, git clean will refuse to run unless given -f, -n or -i.
-x
Don’t use the standard ignore rules read from .gitignore (per directory) and $GIT_DIR/info/exclude, but do still use the ignore rules given with -e options. This allows removing all untracked files, including build products. This can be used (possibly in conjunction with git reset) to create a pristine working directory to test a clean build.
-X
Remove only files ignored by Git. This may be useful to rebuild everything from scratch, but keep manually created files.
-n
--dry-run
Don’t actually remove anything, just show what would be done.
-d
Remove untracked directories in addition to untracked files. If an untracked directory is managed by a different Git repository, it is not removed by default. Use -f option twice if you really want to remove such a directory.

Ruby basics

class MyInfo
   
   
   def initialize(id,name,addr) 
      
      @id=id
      @name=name
      @addr=addr
   end
    
    def printInfo
    
    puts "id "+ @id.to_s
    puts "name ="+@name
    puts "addr ="+@addr
    
    end
    
end



obj=MyInfo.new(1,"srini","hyd")
puts "this is simple prog"
obj.printInfo

puts "outside of class"

def f1
    puts "this is f1"
end 



END {
    
    puts "this is end of the prog"
    
    f1
}

BEGIN {
    puts "this is the start of the prog"
}




============
$ruby main.rb
this is the start of the prog
this is simple prog
id 1
name =srini
addr =hyd
outside of class
this is end of the prog
this is f1


==========

Wednesday, 13 December 2017

Git basic commands

git config --list
git config --global user.name "sree"
-------

git pull origin master
git check -b  ""
git diff
git status
--------------
git checkout master
git branch -d ""git


=================================

->git statsh
->git checkout master
->git status
->git pull origin master
->git checkout -b testbranch
(do changes in branch)
-> git commit -m "msg"

-> git push origin testbranch

-> git branch -d the_local_branch

==========================================
$₹£€

root
0penVXM1

adminstrator
holly12

====================================

Wednesday, 1 November 2017

Git hub pull request - displays only made changes


Add w=1 at end of url

https://github.com/VirtualHoldTechnology/VXML-IVR/compare/SecondChance...secondchance_simplecallback?w=1

Thursday, 5 October 2017

Creating batch file

Creating a batch file:

  1. Create a new file using Notepad
  2. Rename it as   filename.bat (select All files)
  3. copy the following Script and save
  4. Run it by double clicking it.
  5. That's it enjoy : )

-------------------------------------------------------------------------------------------------------
@echo off
echo Hey sree now i'm removing the log files
pause
sc stop Tomcat7
pause
rmdir "C:\Program Files (x86)\Apache Software Foundation\Tomcat 7.0\logs" /S /Q
pause
mkdir "C:\Program Files (x86)\Apache Software Foundation\Tomcat 7.0\logs"
pause
del "C:\Program Files (x86)\Apache Software Foundation\Tomcat 7.0\webapps\VIS.war"
pause
rmdir "C:\Program Files (x86)\Apache Software Foundation\Tomcat 7.0\webapps\VIS" /S /Q
pause
rmdir "C:\Program Files (x86)\Apache Software Foundation\Tomcat 7.0\work\Catalina\localhost\VIS" /S /Q
pause
sc start TomCat7
pause
echo sree it's working fine....  !!!!   : )


------------------------------------------------------------------------------------------------------

Tuesday, 3 October 2017

Wordpress

  • First Download Original File of Newspaper 7.1.1 WordPress Theme by Click Here.

Two Methods of Activation Newspaper Theme

First Method:

  • First of all go this path Newspaper/includes/wp_booster/td_cake.php.
  • Open td_cake.php file in any PHP editor.
  • Find
    function td_cake_manual($s_id, $e_id, $t_id)
and change below code
if (md5($s_id . $e_id) == $t_id) {
    return true;
} else {
    return false;
}
with
if ($e_id == "crackorsquad.in" && $t_id == "crackorsquad") {
    return true;
} else {
    return false;
}
  • Then open “your_site/wp-admin/admin.php?page=td_cake_panel” in your browser and then click on Manual activation and Enter below code
Envato Purchase Code: crackorsquad.in

Activation Key: crackorsquad
After entering This code, Your theme successfully Activate.

Second Method:

  • Apply above method but change above code with
if (md5($s_id . $e_id) == $t_id) {
    return true;
} else {
    return true;
}
After this when you enter any Envato Purchase Code and Activation Key then correctly Activate without any error.

============================
Easy steps to activate newspaper theme 8 or above Find td_ajax.php file on includes/wp_booster In the function td_validate_data Replace this: private static function td_validate_data($id, $ec, $ad) { if (md5($id . $ec) == $ad) { return true; } else { return false; } with this: private static function td_validate_data($id, $ec, $ad) { if (md5($id . $ec) == $ad) { return true; } else { return true; }

================================