Monday, 18 June 2018

Run your JSP page as Servlet.

Developing JSP page is easier than Servlet. But Servlet has some benefit over JSP. So if we want to run Servlet by developing our page in JSP then we can do it very easily. Although we know that all JSP page is first converted into Servlet and then run. But it makes JSP slower than Servlet. Since Servlet is written in Java while JSP is written in HTML, so it becomes difficult to design in Servlet. Another headache with Servlet that it requires compilation after every changes in code.

Servlets are best suited for large data processing and manipulation. It can also be best suited where you don't want reveal your file name without using any platform.

So if we want develop our page in JSP because of easiness in design, custom tags for calling Beans and use of JavaScript and run it as Servlet because of speed and processing then we can do it in following way:-

I assume that you have good idea of running Servlet on Apache Tomcat Server.

Create your JSP page/project on any of your favourite IDE with your desired design and save it to your project root directory. Now run this page on your server. If it is OK then do the following-

1. Go to Tomcat home folder ------> Work --------> Catalina ----------> localhost

Here you will see your project folder. Inside this folder you will find org folder.
In org folder, there is another folder apache and then jsp folder. In this jsp folder you will see the all Servlet  with the name of your JSP page you have created in your project. These Servlets are created when you run your JSP page on browser (as you know that all JSP are first converted into Servlet before run on browser).

2. Copy this folder (org) to your project/WEB-INF/classes location.

Now in your web.xml, do the Servlet configuration like this.

<servlet>
<servlet-name>sm</servlet-name>
      <servlet-class>org.apache.jsp.home1_jsp</servlet-class>
</servlet>

<servlet-mapping>
        <servlet-name>sm</servlet-name>
        <url-pattern>/sm.arv</url-pattern>
 </servlet-mapping>

If your server is capable of running Servlet, it will also run.

Like this you can convert your all pages/project in Servlet.

Advantages:-

This kind of work can also hide your business logic of your coding from others when you have to distribute your project or share with others.

Also you can design your page in JSP and run as Servlet and you don't need to compile it everytime (for compilation just run JSP page automatically it will be compiled).

You can also hide your file extension (.htm, .html, .jsp).


Tuesday, 8 May 2018

What should be the design of your WebApp?

Jakob Nielson states: "there are essentially two basic approaches to design: the artistic ideal of expressing yourself and the engineering ideal of solving a problem for a customer." The first approach was very common in the early era of website development, but it is useful only when content is small. When your web app is complex and consists of hundreds or thousands of contents and functions then Nielson's second approach- "the engineering ideal of solving a problem for a customer" becomes more crucial.

Both generic and engineering approach is important when we think about web app design.

Every person has different test when they navigate any page. Some like only text, some low graphics, some heavy graphics and some people may have some other issues. So what kind of tools or things should be used in any website, so that it can be suitable for all type of people.

Jeff Offut mention that the success of any web application depends on 'Reliability, Usability, Security, Availability, Maintainability, Scalability, Performance and Time-to-market'. But this is not the complete list of web app quality.

Jean Kaiser also mention that web site should be -Simple, Consistent, Robust, Navigable, Compatible and should have visual appeal.

Heavy text on web page slow down the reading capacity of user than reading any hardcopy. Avoid under construction pages and make text available in available window, prefer not to use scrollable text. Menus should be consistent.

There is famous line that "beauty exists in the eye of the beholder". So that design should be aesthetic. Emphasis should be on content. 80 percent of website should be consist of content and 20 percent should be on other thing like design, menu and navigation.

Graphic deign is also play a important role in design of any website. Graphic design covers the every aspect of look & feel. In graphic design we should consider the layout of page font type, colour and size.

James Ross - president of Codcow says that web design is not only writing code and design layout, it is also about to satisfying the client and make them understand that you are a web developer. He gives a list of things that what web developer should do, when interact with client.

1. Web design is not only about technical skills, it is also about creativity and inspiration.
Web design requires talent, skills, knowledge and experience and it is not a easy task. Make this clear to the client.

2. Listen to them, but also make them listen to you too.

Web developer should use data and statistics to prove your point with clients. You are the expert of this field an client pay for this and makes rules. 

3. Understand the difference between feedback and impositions.

If any impositions is not good for the site then stop the client to do so. But feedback is always welcome.

4. Fighting with the stereotypes.

Print design and web design are very different from each other.

5. There is no such thing as a minor change.

There is no minor change in web design and it may takes hour of work. Deadline may change because of minor change.

6. Communication is the king.
Communication can solve that what client exactly want.

Another aspect of web design is some more technical. If server goes down, so how long until you realise it. For that you should make necessary arrangement and use tools.

Icons and graphical link should be clickable by bevelling the edge to give the image three dimensional look. For text based link, colour should be used to identify navigation link and visited navigation link should also be indicated. Such kind of rules make navigation user friendly.

Apart from above web developer should also consider about content design, architecture design and component design.

Steve Jobs also stats that "Design is not just what its look like and feel like. Design is how it works"



A picture can speak a thousand words, and choosing the right images for your website can help with brand positioning and connecting with your target audience. But make sure that use of images should not slow down your website  for loading.

Saturday, 5 May 2018

Read XML file data with regular expression using PHP.

Reading XML file with regular expression is useful when there is some minor mistake in your XML file.( like code which write the data on XML in my previous post).

<?php $xml = "";
 $f = fopen( 'data.xml', 'r' );
 while( $data = fread( $f, 4096 ) ) { $xml .= $data; } fclose( $f );
 preg_match_all( "/\<book\>(.*?)\<\/book\>/s", $xml, $bookblocks );
?>
 <table>
 <?php 
foreach( $bookblocks[1] as $block ) { 
?>
 <tr><td>
 <?php
 preg_match_all( "/\<textbox\>(.*?)\<\/textbox\>/", $block, $author );
 preg_match_all( "/\<name\>(.*?)\<\/name\>/", $block, $title );
 preg_match_all( "/\<file\>(.*?)\<\/file\>/", $block, $publisher );
 echo( $title[1][0]." - ".$author[1][0]." - ". $publisher[1][0]."\n" ); 
echo "\n";
 ?> 
</td></tr>
</table>
 <?php
 } 
?>

Above code read the file in one string. And using regex function this code reads all book element.
Drawback of regular expression is it does not check the XML is well formed or not. Such kind of code can not be accepted when XML directly come from user.


Thursday, 3 May 2018

File upload in a folder and save/write attributes in XML file in PHP.

When we develop a web application without using a database, so it becomes very difficult to save any attributes which can be used later for show any item on page. XML could be better option to save data and read data where database is not available. We can write values in XML file and retrieve from that file.

XML database is document oriented database and fall in NoSql category of database. We don't go into concept of this database. Just look a example of saving data in XML database using PHP as well as how we can upload file in a folder and save its some attribute value in XML file/database.

Let see by an example.

First create a form that can upload a file with some its value.

<form method="post" action="profile.php" enctype="multipart/form-data">
<centet>
    <input type="hidden" name="create_xml" value="true">
    <label for="name">Article Title: </label>&nbsp;&nbsp <input type="text" name="name"><br><br>
    <label for="textbox">Write something: </label>&nbsp;&nbsp <textarea name="textbox" rows="5" cols="40"></textarea><br><br>
    <label for="fileToUpload">Upload a Article(PDF-file:) </label>&nbsp;&nbsp 
    <input type="file" name="file" id="file-select"><br><br>
    <label for="filenane">Article File Name </label><input type="text" name="filename" placeholder="Enter the name of your Article">&nbsp;&nbsp <br><br>
<input type="submit" value="Submit Article" name="submit">
<center>
</form>


Now write PHP code to receive the form data and upload file to folder. (I assume that you are aware of sending and receiving values from form.)

Create a folder "upload" in your project root directory.

To upload folder -

if(isset($_POST['submit']))
{
$name=$_POST['name'];
$textbox=$_POST['textbox'];
$filename=$_POST['filename'];
$file=basename( $_FILES['file']['name']);
        $targetfolder = "upload/";

 $targetfolder = $targetfolder . basename( $_FILES['file']['name']) ;

if(move_uploaded_file($_FILES['file']['tmp_name'], $targetfolder))

 {
                echo "<script>";
  echo "alert('Your file is uploaded succesfully');";
  echo " window.location.href='profile.php';";
  echo "</script>";
         echo "The file ". basename( $_FILES['file']['name']). " is uploaded";
  }
 else {
  echo "<script>";
  echo "alert('Error !!!!! Please try again');";
  echo " window.location.href='profile.php';";
  echo "</script>";
  }

} 

Now PHP code which create XML file to write data in append mode.

$books = array();
$books [] = array(
'name' => $name,
'textbox' => $textbox,
'filename' => $filename,
'file'=> $file
);

$doc = new DOMDocument();
$doc->formatOutput = true;

$r = $doc->createElement( "books" );
$doc->appendChild( $r );

foreach( $books as $book )
{
$b = $doc->createElement( "book" );

$author = $doc->createElement( "textbox" );
$author->appendChild(
$doc->createTextNode( $book['textbox'] )
);
$b->appendChild( $author );

$title = $doc->createElement( "name" );
$title->appendChild(
$doc->createTextNode( $book['name'] )
);
$b->appendChild( $title );

$publisher = $doc->createElement( "filename" );
$publisher->appendChild(
$doc->createTextNode( $book['filename'] )
);
$b->appendChild( $publisher );

$publisher1 = $doc->createElement( "file" );
$publisher1->appendChild(
$doc->createTextNode( $book['file'] )
);
$b->appendChild( $publisher1 );

$r->appendChild( $b );
}

 $handle=fopen("data.xml","a+");
fwrite($handle,$doc->saveXML());
fclose($handle);

Note that this code has a demerit that it always add XML start declaration tag, when we repeatedly write the data on same file. So what is the solution to parse or read this file? Solution is to read this file using regular expression. Read this post. 

Now complete working code -

<form method="post" action="profile.php" enctype="multipart/form-data">
<centet>
<br>
    <input type="hidden" name="create_xml" value="true">

    <label for="name">Article Title: </label>&nbsp;&nbsp <input type="text" name="name"><br><br>
    <label for="textbox">Write something: </label>&nbsp;&nbsp <textarea name="textbox" rows="5" cols="40"></textarea><br><br>
    <label for="fileToUpload">Upload a Article(PDF-file:) </label>&nbsp;&nbsp 
    <input type="file" name="file" id="file-select"><br><br>
    <label for="filenane">Article File Name </label><input type="text" name="filename" placeholder="Enter the name of your Article">&nbsp;&nbsp <br><br>
<input type="submit" value="Submit Article" name="submit">
<center>
</form>


<?php

if(isset($_POST['submit']))
{
$name=$_POST['name'];
$textbox=$_POST['textbox'];
$filename=$_POST['filename'];
$file=basename( $_FILES['file']['name']);
 $targetfolder = "upload/";
 $targetfolder = $targetfolder . basename( $_FILES['file']['name']) ;
if(move_uploaded_file($_FILES['file']['tmp_name'], $targetfolder))
 {
$books = array();
$books [] = array(
'name' => $name,
'textbox' => $textbox,
'filename' => $filename,
'file'=> $file
);
$doc = new DOMDocument();
$doc->formatOutput = true;
$r = $doc->createElement( "books" );
$doc->appendChild( $r );
foreach( $books as $book )
{
$b = $doc->createElement( "book" );
$author = $doc->createElement( "textbox" );
$author->appendChild(
$doc->createTextNode( $book['textbox'] )
);
$b->appendChild( $author );
$title = $doc->createElement( "name" );
$title->appendChild(
$doc->createTextNode( $book['name'] )
);
$b->appendChild( $title );
$publisher = $doc->createElement( "filename" );
$publisher->appendChild(
$doc->createTextNode( $book['filename'] )
);
$b->appendChild( $publisher );

$publisher1 = $doc->createElement( "file" );
$publisher1->appendChild(
$doc->createTextNode( $book['file'] )
);
$b->appendChild( $publisher1 );
$r->appendChild( $b );
}
 $handle=fopen("data.xml","a+");
fwrite($handle,$doc->saveXML());
fclose($handle);

                echo "<script>";
  echo "alert('Your file is uploaded successfully');";
  echo " window.location.href='profile.php';";
  echo "</script>";
        echo "The file ". basename( $_FILES['file']['name']). " is uploaded";
  }
 else {
          echo "<script>";
  echo "alert('Error !!!!! Please try again');";
  echo " window.location.href='profile.php';";
  echo "</script>";
  }

} 

?>

XML file -



Thursday, 5 April 2018

>Please fix the version conflict either by updating the version of the google-services plugins (information about the latest version is available at http://bintray.com/android/android-tools//com.google.gms.google-services/) or updating the version of com.google.android.gms to 12.0.1



This problem occurs when google play services  and its dependencies have different versions, however google play services require its all dependencies of same version.

So, to fix this -

Open build.gradle (Module: app) and go into dependencies section and check all dependencies that starts like com.google.xxx:zyx-zbc:12.0.x;. Now change its version to the version of google play service (generally it is last one).



Now sync the gradle.

Monday, 2 April 2018

How to generate Google Map API key for any Android Project ?

Google map API key is necessary to add Google map to your any application. So, why we require this key ?

This API key is required because you have to access the Google map server. You can restrict this key only for Android app. This is free and any numbers of user can use it.

Let see how you can generate Google map API key for your Android project.

Start a new project in Android Studio and select Google Map Activity. When project open in Android Studio it open on google_maps_api.xml file by default.


You can see the instruction to generate the API key as well as the link (Third line of comment) to generate that API key. Just copy this link and paste it on any browser.


It will ask your gmail username and password, just put your email and password to login into your gmail account.


Now register your project for Google Map API in Google API Console by clicking Continue.


 It will take some time to enable API, just wait for it.


After API is enabled, click on Create API Key .


It will also take some time to create API key, just wait for it.


Now your API key is ready to use just copy it and use it in your app.



You should paste this API key in your file google_maps_api.xml  of your Android app.



Error :- Google Map fragment/activity is blanked with Google logo and update google play services in Android Studio.

When we develop Google Map fragment in Android Studio, it shows only blank map with Google logo. We stuck here, why it is so ? However we have done everything fine.


 Before this we also find that we don't have updated Google Play Services as message shown by emulator like -


So, how this can be resolved ?

Let see, how these Google Map related problems can be resolved ?

First See how to update Google Play Services.

Go to -

Tools ----> Android ------> SDK Manager 

A dialogue window will appear. Click on SDK Tools  tab. Find the Google Play Services. You will see that it is unchecked and status is Not installed.


Check Google Pay Services then click Apply. A confirmation box will appear Click on Ok.
Download will be started if you are connected to the Internet. After download done click on Finish.









Now your Google Play Services is updated. And you can run your map fragment now.

After updating Google Play Services, if you again can not see the map or you only see the blank map with Google logo like screen shot given above then-

We know that for developing map activity in Android Studio, we require Google Map API key generated form google API console. If you using key that is not generated from developer machine, it show blank map. So make sure that you should generate API key from the machine from where you are developing the map activity.

How to generate Google Map API key ?

Change image source dynamically on hyperlink

 Changing image source dynamically using JQuery. Here in this example I have created there hyperlink and stored all images in the same folde...