Wednesday, February 26, 2020

Invalidate Session on Spring @ControllerAdvice

Problem:

When some exception comes in spring web application, some times we have to redirect the page some exception jsp/html page .
we have to fix this problem in ControllerAdvice Class.

Solution:

        
    @ExceptionHandler(ResourceAccessException.class)
    public ModelAndView handleResourceAccessException(ResourceAccessException ex,HttpServletRequest request) {
      LOG.error(START);
      HttpSession session= request.getSession(false);
        SecurityContextHolder.clearContext();
         if(session != null) {
             session.invalidate();
         }
        ModelAndView model = new ModelAndView();
        LOG.error(MESSAGE,ex.getMessage());
        model.setViewName("generic_page");
        LOG.error(END);
        return model;
    }
  • we get the session Object from HttpServletRequest object and checks the null condition and after that we have invalidated the session.
  • Important point

    is that we have write this statment : SecurityContextHolder.clearContext(); otherwise Spring Security will not identify our session invalidation.

Bootstrap Scroll able Menu and selection of data value and text

        
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
<script src="https://code.jquery.com/jquery-1.11.1.min.js"></script>
<link href="https://netdna.bootstrapcdn.com/bootstrap/3.1.0/css/bootstrap.min.css" rel="stylesheet" id="bootstrap-css">
<script src="https://netdna.bootstrapcdn.com/bootstrap/3.1.0/js/bootstrap.min.js"></script>


<style>
  .scrollable-menu {
      height: auto;
      max-height: 200px;
      overflow-x: hidden;
  }
</style
</head>
<body>
<div class="container">
    <div class="row">
        <div class="col-lg-12">
            <h2 class="bold">Scrollable Menu</h2>
            <div class="btn-group">
                <div class="dropdown">
  <button  style="max-width: 300px;width: 200px;"class="btn btn-default dropdown-toggle" type="button" id="dropdownMenu1" data-toggle="dropdown" aria-haspopup="true" aria-expanded="true">
    Dropdown
    <span class="caret"></span>
  </button>
  <ul class="dropdown-menu scrollable-menu" aria-labelledby="dropdownMenu1">
    <li><a href="#" data-value="Nagaraju">Action</a></li>
    <li><a href="#" data-value="another action">Another action</a></li>
    <li><a href="#" data-value="something else here">Something else here</a></li>
    <li><a href="#" data-value="separated link">Separated link</a></li>
<li><a href="#" data-value="action">Action</a></li>
    <li><a href="#" data-value="another action">Another action</a></li>
    <li><a href="#" data-value="something else here">Something else here</a></li>
    <li><a href="#" data-value="separated link">Separated link</a></li>
<li><a href="#" data-value="action">Action</a></li>
    <li><a href="#" data-value="another action">Another action</a></li>
    <li><a href="#" data-value="something else here">Something else here</a></li>
    <li><a href="#" data-value="separated link">Separated link</a></li>
  </ul>
</div>
            </div>
         

        </div>
    </div>
</div>
</body>
<script>
  $(function() {
  $(".dropdown-menu li a").click(function(){
    $(this).parents(".dropdown").find('.btn').html($(this).text() + ' <span class="caret"></span>');
    console.log($(this).attr('data-value'));
    $(this).parents(".dropdown").find('.btn').val($(this).data('value'));
  });
  })
</script>
</html>

User creation on MySQL and giving Privileges


mysql> CREATE USER 'good'@'localhost' IDENTIFIED BY 'man';
Query OK, 0 rows affected (0.16 sec)

mysql> GRANT ALL ON iot.* TO 'good'@'localhost';
Query OK, 0 rows affected (0.18 sec)

mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.12 sec)
-------------------------------------------------------------------------------------------------------------------------

Here localhost means that  Data Pushing Application and MySQL DB Server both are on the same System.
--------------------------------------------------------------------------------------------------------------------------
If Data Pushing Application  and MySQL DB Server are on different Systems.

Then we give  the permission to that  Data Pushing Application like ..
CREATE USER 'good'@’2.16.1.200’ IDENTIFIED BY 'man';
GRANT ALL ON iot.* TO 'good'@'2.16.1.200';   (Here 2.16.1.200 is the Data Pushing Application IP Address.)
---------------------------------------------------------------------------------------------------------------------------------------------------------------------
If we want to access any host(IP Address of any System) then
 CREATE USER 'good'@’%’ IDENTIFIED BY 'man';
GRANT ALL ON iot.* TO 'good'@'%';   (Here % means that Any host can access our MySQL Server DB)
-------------------------------------------------------------------------------------------------------------------------

Tuesday, February 25, 2020

Learning any Technology


The following are the main things to learn any technology.


  1. Go to its official Web Site and learn the Getting Started and then read the complete documentation.
  2. Read any best book on that technology.
  3. Teach to any body about this technology which you have learned.
  4. Practice that technology.
  5. Revise the technology at some intervals.



JQuery Hide/Show -while refreshing the page showing the hidden division

Problem : 

        we have given two divs  -
                                  <div id="dashboard">
Dashboard </div> <div id="setting"> Setting </div>

        From these two divisions ... I have hidden the setting division by using the following .

                              $("#setting").hide();

       When I fresh the F5 button or refresh the page, the  setting division is showing for some seconds and disappearing.

Solution : 

I have tried many but nothing works.
So I have given written the following.


<div id="dashboard"> Dashboard </div> <div id="setting" style="display:none;"> Setting </div>
                               
After writing the style to setting div , then code is working properly.


   

Recent Post

Databricks Delta table merge Example

here's some sample code that demonstrates a merge operation on a Delta table using PySpark:   from pyspark.sql import SparkSession # cre...