Monday, 22 October 2018

Show / Print a variable value in input type field in Servlet.

Generally in Servlet we use HTML in Java (Java --->HTML), but in above we have to write Java ----> HTML -----> Java. So, we can write this in following manner

A simple Sevlet code can be like -

out.println("<table border='1' cellspacing='3' cellpadding='2'>");

If we want double quote instead of single quote in HTML properties value then code will be -

out.println("<input type=\"hidden\" name=\"place\" value=\"any value\">");

And finally when we want any variable value in value property of input type, then code will be -

out.println("<input type=\"hidden\" name=\"place\" value=\""+ variable +"\">");

Saturday, 20 October 2018

Make back button working in action bar in Android.

Recently I was trying to use working back button on my one of the project, but whatever I was done previously was not working. It was a crash every time with error message -


I am newbie to Android, but I was done back activity few day back but after updating my Android Studios and APIs nothing was working. I was also uses  inflated menu in my project, and debugger always stuck in onOptionsItemSelected(MenuItem item) method which I was used for option menu. I also tried many unsuccessful lines of code. Finally I got this solution for my problem -

1 .  In  onCreate(Bundle savedInstanceState) method use -

getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);

2 .  Override methos in your activity class

@Overridepublic boolean onSupportNavigateUp() {
    onBackPressed();
    return true;
}


3 . In onOptionsItemSelected(MenuItem item) method use -

switch (item.getItemId()) {  
       case android.R.id.home:
        Intent i = new Intent(this, Your_Parent_Activity.class);
        intent.addFlags(i.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(i);
        return true;
}


Monday, 8 October 2018

File listing and download using hyperlink in JSP from any folder.

Why I am writing this blog? In network some times we don't have server/FTP server(I personally think that it is not more interactive, i.e it can't be in a form of web page) and we have to share any file/s to more than 50 people, then we stuck in messages like "No more connections can be made.. bla bla bla" or either network restriction policy don't allow to do so. Apart from this there may be other issues that can stop to access shared folders in network. I have seen all these kind of problem in accessing shared documents.

So I tried to make web page like application where user can view and download their files via web page. For that you require obviously Tomcat Server for running JSP pages.

So, you should have working knowledge of Tomcat Server.

Create a JSP page and save in your project directory and use this code.

   <p><% 
File folder = new File("G:\\tomcatserver\\webapps\\softshare\\share\\fonts");
File[] listOfFiles = folder.listFiles();
%><table><%
     for (int i = 0; i < listOfFiles.length; i++) {
      if (listOfFiles[i].isFile()) {
String pp="share/fonts/"+listOfFiles[i].getName();
%><tr><td width="500px"><a href="<%=pp%>"><%=listOfFiles[i].getName()%></a><%
   %></td><td><%
Path path=Paths.get(listOfFiles[i].getPath());
BasicFileAttributes attr = Files.readAttributes(path, BasicFileAttributes.class);
out.println(attr.creationTime().toString().substring(0,10));
out.println(" </td></tr>");
      }
    }
%></table><%
 %></p>

Now lets see the code in details-

File folder = new File("G:\\tomcatserver\\webapps\\softshare\\share\\fonts"); //path of folder
File[] listOfFiles = folder.listFiles(); // will store file in array.

This code will read the files of fonts/ folder. The font folder should be in your project directory, it should not be out your tomcat server.

 String pp="share/fonts/"+listOfFiles[i].getName();

pp will store the relative path of files.

Now you will be able to list and download your file via hyperlink.

Thursday, 4 October 2018

Add app bar / action bar / tool bar and menu in Android Studio.

App bar or action bar or tool bar is a dedicated bat on the top of the every app/activity. It is also called title bar of activity. It may contain toolbox/icon or overflow menu or can be both.


So how can we create this action bar in your project?

1. create menu.xml file in res/menu directory of your project. To create go to -

New ---> Android Resource File

Enter the file name (should start with small letters alphabets) then select resource type as menu then click Ok.


Now create menu by adding items in this menu.xml file.

<item android:title="Item 1"></item>
<item android:title="Item 2"></item>
<item android:title="Item 3"></item>
<item android:title="Item 4"></item>

The complete menu.xml is-

<?xml version="1.0" encoding="utf-8"?><menu xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:app="http://schemas.android.com/apk/res-auto">
    <item android:title="Item 1"></item>
    <item android:title="Item 2"></item>
    <item android:title="Item 3"></item>
    <item android:title="Item 4"></item>
</menu>


2. After creating menu, you require it to inflate on title bar/action bar. To inflate, write given below code in you activity

 @Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater in=getMenuInflater(); in.inflate(R.menu.menu,menu); return super.onCreateOptionsMenu(menu); }

After running your program you can see the menu in action bar, when you click on three dots on left side of action bar like -


If you want to add icon or tool box on the action bar then in menu.xml file, use icon property of item tag and select any .png image from your res/mipmap directory, like (You can see home icon in screen shot given below)  

<item android:title="home"    android:icon="@mipmap/home"    app:showAsAction="always"></item>




Error: Cannot fit requested classes in a single dex file (# methods: 87573 > 65536) When you try to generate apk in Android Studio 3.2

After updating my Android Studio from 3.0.1 to 3.2 and today when I tried to generate apk to run my project in my android phone, I stuck on error:-

"Error: Cannot fit requested classes in a single dex file (# methods: 87573 > 65536)"

After searching on internet I found this working solution for me.

Let see the screen shot -



To resolve this -

Go to app level build.gradle and add this dependency under dependencies { }

implementation 'com.android.support:multidex:1.0.3'

also add this line in defaultConfig { } section

multiDexEnabled true 




and then click sync now.



Now your problem is resolved.

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...