tag:blogger.com,1999:blog-1603182231993335412024-02-20T02:44:07.544+01:00Technology For HumanComputers, Java programming - for an ordinary human being, simple as possible.Paweł Nieściorukhttp://www.blogger.com/profile/12640446237233312925noreply@blogger.comBlogger31125tag:blogger.com,1999:blog-160318223199333541.post-38439370561268910132022-04-08T11:23:00.004+02:002022-04-08T14:00:55.260+02:00Git for Windows - tips<p>Some time ago I described how to install Git For Windows and how to use it in IntelliJ Idea. See <a href="https://technology-for-human.blogspot.com/2019/01/gather-all-hammers-inside-toolbox.html">here</a> for details. I realised that from time to time I go back to those things:</p><p></p><ul style="text-align: left;"><li>modifying git config files and trying to find them</li><li>working with passwords</li><li>using different editor for commit message</li></ul><p></p><p>Let's take a closer look.</p><h1 style="text-align: left;">Modifying git config files and trying to find them.</h1><p>Git uses three different config files (usually named .gitconfig) at three different levels: </p><h2 style="text-align: left;"><b>System</b></h2><div><b><br /></b></div><div><b>Location: </b><span style="font-family: courier;">[your_git_for_windows_instalation_path]/etc/.gitconfig</span> or <span style="font-family: courier;">[your_git_for_windows_instalation_path]/mingw64/etc/.gitconfig</span>. </div><div><br /></div><div><b>Purpose:</b> contains values applied to every user on the system and all their repositories. If we want to read/write from/to this file we have to pass <span style="font-family: courier;">--system</span> option. Because this is a system configuration file, you may need superuser privilege to make changes to it.</div><h2 style="text-align: left;"><br /></h2><h2 style="text-align: left;">Global</h2><div><br /></div><div><b>Location:</b> <span style="font-family: courier;">[windows_user_directory]/.gitconfig</span></div><div><br /></div><div><div><b>Purpose:</b> contains values applied to certain (usually logged in) user on the system and all repositories this user works with. If we want to read/write from/to this file we have to pass <span style="font-family: courier;">--global</span> option.</div><h2 style="text-align: left;"><br /></h2><h2 style="text-align: left;">Project</h2></div><div><br /></div><div><b>Location:</b> <span style="font-family: courier;">[project_directory]/.gitconfig</span></div><div><span style="font-family: courier;"><br /></span></div><b>Purpose: </b>contains values applied to certain project. If we want to read/write from/to this file we have to pass <span style="font-family: courier;">--local</span> option, or use no option. When changing values in certain project/git repository we have to execute this command being located in that project.<div><div><div><div><br style="background-color: white; color: #222222; font-family: Arial, Helvetica, sans-serif; font-size: small;" /></div></div></div></div><h1 style="text-align: left;">Working with passwords.</h1><div><br /></div><div>During installation process Git For Windows offers to install Git Credential Manager (GCM) - a tool for storing passwords in a secured way. </div><div><br /></div><div>But what if want to enter password every time when we push/pull code from remote repository? We can select "None" when Git installer asks for manager. But what if we already installed it and we want to remove it? We have to entirely remove below entry from system config file: </div><br /><span style="font-family: courier;">[credential] </span><div><span style="font-family: courier;"><span> </span>helper = manager</span><br /><br />Git will start to ask for passord every time, but it will do it using OpenSSH popup window. If we want to disable it in order to enter password directly from command line (or in terminal window in IntellJ), we have to add empty entry for system config file by executing this command: </div><div><br /></div><div><span style="font-family: courier;">git config --system core.askpass ""</span></div><div><br /></div><div>If You really want to not to enter password every time during pull/push with remote repository it is better to use SSH keys.</div><div><br /></div><h1 style="text-align: left;">Using different editor for commit message.</h1><div><br /></div><div>By default Git For Windows offers to install vim as a default editor for commit messages. It is XXI century, we have many great editors under Windows, so do not do this mistake and do not install this archaic editor for linux geeks ;) Why? Let's demonstrate it with example.</div><div><br /></div><div><b>1.</b> We have a "Hello World" java application maintained by Git:</div><div><br /></div><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjUWdRMfqlMoikp2toDHMIcEkRITzZKsNGFNXD8tI_plX_A2_Hb_GY1FlU2aguEYPq75MZQCFJPdFnulYmhUf2BXd686lGtuHO8HQ4_-L1AL0r_xplpEmyZq5j_MuIacS0kxHja-iy1a-YXgl-r8G8LAav1y-X-w-MyGZBgIGGB9pJiegEN7O7BlNXehg/s1436/01.PNG" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="631" data-original-width="1436" height="282" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjUWdRMfqlMoikp2toDHMIcEkRITzZKsNGFNXD8tI_plX_A2_Hb_GY1FlU2aguEYPq75MZQCFJPdFnulYmhUf2BXd686lGtuHO8HQ4_-L1AL0r_xplpEmyZq5j_MuIacS0kxHja-iy1a-YXgl-r8G8LAav1y-X-w-MyGZBgIGGB9pJiegEN7O7BlNXehg/w640-h282/01.PNG" width="640" /></a></div><br /><div><b>2.</b> When we commit changes and in the commit command we pass commit message, we will not find any issues with the vim:</div><div><br /></div><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhIOGxNlT41rE5_k7Zvn9a23nRH4mkU4X9lt42fHb8GO4PliaLUfAwvEqU--iAziNtIX-F8elhV356i50cgbQye7xni3c3y68p0O8mInyk0MGKjHyITDbiAVT2fKmxZEvPyT4DTrIoMOSlB9qBmRHXtubPt7wdT1OxEWnzynaTYhDQefV2aLn6awbeJiw/s1247/02.PNG" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="776" data-original-width="1247" height="398" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhIOGxNlT41rE5_k7Zvn9a23nRH4mkU4X9lt42fHb8GO4PliaLUfAwvEqU--iAziNtIX-F8elhV356i50cgbQye7xni3c3y68p0O8mInyk0MGKjHyITDbiAVT2fKmxZEvPyT4DTrIoMOSlB9qBmRHXtubPt7wdT1OxEWnzynaTYhDQefV2aLn6awbeJiw/w640-h398/02.PNG" width="640" /></a></div><br /><div><b>3.</b> But let's use a commit command where no commit message is passed and we expect an editor to open and let us put message there. W use <span style="font-family: courier;">git commit</span> command without "-m" option:</div><div><br /></div><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhEpoo34fsmjmYVgaiO3ECQTY1IXyLY3UJNGgE_JW15xeOvMWWQDs3G8i7l9NGWXHyj8anhjCEZE_P3BNDxaZe0VlVXjp6BiEjVswoNjU0DThD5ouhYqaJz5Ma7fxSytb0DlIo327ZbHIJ8uUleHds8psWXp6FI8_ikoOIGYk1EHMQCi0C1znqgsXFbsg/s1397/03.PNG" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="824" data-original-width="1397" height="378" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhEpoo34fsmjmYVgaiO3ECQTY1IXyLY3UJNGgE_JW15xeOvMWWQDs3G8i7l9NGWXHyj8anhjCEZE_P3BNDxaZe0VlVXjp6BiEjVswoNjU0DThD5ouhYqaJz5Ma7fxSytb0DlIo327ZbHIJ8uUleHds8psWXp6FI8_ikoOIGYk1EHMQCi0C1znqgsXFbsg/w640-h378/03.PNG" width="640" /></a></div><br /><div>As we see vim is opened in the terminal and after start insertion mode (press Insert) we can type in commit message. Now we have to save the commit message. As You know we have to press Esc in vim to exit insert mode and type something like :wq!. </div><div><br /></div><div>Good luck :) When You press Esc focus is switched to the Main.java file instead of terminal where vim is opened. It is a known issue in IntelliJ when using Git in its terminal: https://stackoverflow.com/questions/67848200/how-do-i-commit-a-git-message-in-intellijs-terminal</div><div><br /></div><div>In short: You have to change Esc key binding to switch focus under IntelliJ (assign different key not only remove Esc). Or if You have luck and do left-click (on some area different than file edit window) with Esc, You may sooner or later exit from insert mode:</div><div><br /></div><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjNDC-Y3Yb74IOHjvukQHYOBrV9XYJsScS9dArc1oQGjRSak8kOucJwLqMebVB5GIcgbgM58TZzH0ia85Wdi4LMtR2X7x0usafqv6gxezKxX-P_45HzAR_4ZTum4y7fL0gdaB0D94lMlbyNVNFSuCsnTxLqp8G3v0eN5EDiGnbBO_c693lLsnDdbJxwyw/s1552/04.PNG" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="1343" data-original-width="1552" height="554" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjNDC-Y3Yb74IOHjvukQHYOBrV9XYJsScS9dArc1oQGjRSak8kOucJwLqMebVB5GIcgbgM58TZzH0ia85Wdi4LMtR2X7x0usafqv6gxezKxX-P_45HzAR_4ZTum4y7fL0gdaB0D94lMlbyNVNFSuCsnTxLqp8G3v0eN5EDiGnbBO_c693lLsnDdbJxwyw/w640-h554/04.PNG" width="640" /></a></div><br /><div>After typing :wq! we see that commit is complete with our message:</div><div><br /></div><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjQ7I0GhpRUX9iVVUjS-4AfqiOFck4TARdjnwhiyqx_JV6-K51eEtt2EM9xapQd_Q8iCPbedPykhYOazgY68IDQEOPVVo4hrbVj4uSB4swiujSGjVLXMvqp3dohcS7zCYMo4ZAHpm_GLIvYHI9ch3REpSvwdvo-xxYXvl-IdlEnpSSB5nXOR_NbL7EUNQ/s947/05.PNG" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="250" data-original-width="947" height="168" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjQ7I0GhpRUX9iVVUjS-4AfqiOFck4TARdjnwhiyqx_JV6-K51eEtt2EM9xapQd_Q8iCPbedPykhYOazgY68IDQEOPVVo4hrbVj4uSB4swiujSGjVLXMvqp3dohcS7zCYMo4ZAHpm_GLIvYHI9ch3REpSvwdvo-xxYXvl-IdlEnpSSB5nXOR_NbL7EUNQ/w640-h168/05.PNG" width="640" /></a></div><br /><div>You can now say: "What a problem, just change this binding if You still want to use git from terminal. Or just use excellent graphic integration with git from IntelliJ". I would say: "Maybe it is high time use excellent external editor which I use every day instead of archaic one which I have to configure in unusual way".</div><div><br /></div><div><b>4.</b> <b>Solution:</b> use external editor instead of vim (here I will use Sublime).</div><div><br /></div><div>First we define default editor in git system config file using this command:</div><div><br /></div><div><span style="font-family: courier;">git config --system core.editor "'C:/Dodatkowe/Sublime/subl.exe' -w"</span></div><div><br /></div><div>Now we check if there are any entries about core.editor in global config file and specific project config file (which may override system setting). If we find any, remove them. Now let's try to use Sublime as editor:</div><div><br /></div><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhjKB0EFRxIdagyIJTuoOIgKEnXvbEbgpZfHORgq_Z9ZT5BA7HLQjF8XQvSU_JHqKT6WdlUOHKdKUVR8HH1ossuTJzyWp4ISdwrT2-vds2ELes1K5mpdi-_kZsNe72Kt3YKsl7lHEBJ8rQa80GQXjBeYnEgzIyRHlswT95QKVi6SXfIzw3DsH2-dZwRmQ/s2189/06.PNG" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="1064" data-original-width="2189" height="312" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhjKB0EFRxIdagyIJTuoOIgKEnXvbEbgpZfHORgq_Z9ZT5BA7HLQjF8XQvSU_JHqKT6WdlUOHKdKUVR8HH1ossuTJzyWp4ISdwrT2-vds2ELes1K5mpdi-_kZsNe72Kt3YKsl7lHEBJ8rQa80GQXjBeYnEgzIyRHlswT95QKVi6SXfIzw3DsH2-dZwRmQ/w640-h312/06.PNG" width="640" /></a></div><br /><div>After typing in <span style="font-family: courier;">git commit</span><span style="font-family: inherit;"> Sublime editor is opened in new window, where we can type in our commit message. Please notice what IntelliJ displays in terminal: </span></div><div><span style="font-family: courier;"><br /></span></div><div><span style="font-family: courier;">hint: Waiting for your editor to close the file...</span></div><div><br /></div><div>Yes, it is wating until You save the file and close Sublime (when using vim it is also display this message, but exiting there is harder ;)). So lets save the file by Ctrl+S and close the Sublime:</div><div><br /></div><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhPujHBvq83GK-bwWaM9hcuBJ-B_eQWJDY_nDWykdlhSYAU37PEdjW_upLx_M2WSAn9mENtjlaqegG_vFChjwUPzMkZkPV_NHplkW-6SgS1-dAyyHMVoJ4Je5pyZRpdniY__it306Jhqev5QqBCiW9OVHnAdb2oNKFxpCykFQDZZG3uF6HXtDvF8fh-jg/s767/07.PNG" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="238" data-original-width="767" height="198" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhPujHBvq83GK-bwWaM9hcuBJ-B_eQWJDY_nDWykdlhSYAU37PEdjW_upLx_M2WSAn9mENtjlaqegG_vFChjwUPzMkZkPV_NHplkW-6SgS1-dAyyHMVoJ4Je5pyZRpdniY__it306Jhqev5QqBCiW9OVHnAdb2oNKFxpCykFQDZZG3uF6HXtDvF8fh-jg/w640-h198/07.PNG" width="640" /></a></div><br /><div><br /></div><div>It works without changing anything in IntelliJ.</div><div><br /></div><div><br /></div>Paweł Nieściorukhttp://www.blogger.com/profile/12640446237233312925noreply@blogger.comtag:blogger.com,1999:blog-160318223199333541.post-56240149860859805862021-08-24T13:21:00.003+02:002023-06-11T08:04:38.867+02:00Java Maven project error: Source option 5 is no longer supported. Use 6 or later.<h3 style="text-align: left;">Problem:</h3><div><br /></div><div><div>Suppose we are building an application from scratch based on Maven and JDK 11 and our pom.xml file contains nothing but groupId/artifactId/version information. An attempt to compile will end with an error as in the title.</div><div><br /></div><div>The same thing will happen if we have an application in Java 1.5 (yes, it happens...) and we try to upgrade JDK version to e.g. 1.8 or 11. Changing Java version in the project and trying to compile will also end with this error.</div><div><br /></div><h3 style="text-align: left;">Requirements:</h3></div><div><ul style="text-align: left;"><li>installed IntelliJ, JDK and Maven as described <a href="https://www.blogger.com/#">here</a>. We need Java 11 to be installed.</li></ul><div><br /></div><h3 style="text-align: left;">Solution:</h3><div><br /></div><div>Let's try to repeat this mistake by creating simple "Hello World" application wchich is based on Maven and uses JDK 11 from Amazon:</div><div><br /></div><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEikzw-e-SUVirTyl2exL5qQtHFWvtBWTU7twJLlRnmpVtZYpdzvUVAQV2cnABATGUO_DDYt3UbCW0J26Q1s2z5l8OrfDRqsF52EXIatvDhDiTgVtIsoNibByZIuNf-XrZ_YDKr5dg8PZInd/s2494/01.png" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="1134" data-original-width="2494" height="183" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEikzw-e-SUVirTyl2exL5qQtHFWvtBWTU7twJLlRnmpVtZYpdzvUVAQV2cnABATGUO_DDYt3UbCW0J26Q1s2z5l8OrfDRqsF52EXIatvDhDiTgVtIsoNibByZIuNf-XrZ_YDKr5dg8PZInd/w400-h183/01.png" width="400" /></a></div><br /><div class="separator" style="clear: both; text-align: center;"><br /></div>Now let's try compiling it with Maven using the "mvn clean package" command in terminal (You can also do it by clicking in Idea's Maven window):</div><div><br /></div><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjpST2Rarw9PXFRj5l6TK2xgP84F65t4WYkSkiNOsRIzOqkmkOaJvfbzSNFZSdZflvDfKmzD132OKrrPjmTwFAfjKTvTITKymncCZ9XFiBWi0SoMqqXZ3h4o0JU9IdJI5i3ijCGxWlaJndT/s1468/02.png" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="1259" data-original-width="1468" height="343" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjpST2Rarw9PXFRj5l6TK2xgP84F65t4WYkSkiNOsRIzOqkmkOaJvfbzSNFZSdZflvDfKmzD132OKrrPjmTwFAfjKTvTITKymncCZ9XFiBWi0SoMqqXZ3h4o0JU9IdJI5i3ijCGxWlaJndT/w400-h343/02.png" width="400" /></a></div><br /><div><br /></div><div>Tip: if you're wondering why I have such nice colors in the terminal, let me remind you that I set up the Idea terminal to use Git Bash underneath (described <a href="https://technology-for-human.blogspot.com/2019/01/gather-all-hammers-inside-toolbox.html">here</a>). However when You use standard Windows Terminal (cmd.exe) or Windows PowerShell as a Terminal underneath colors also work.</div><div class="separator" style="clear: both; text-align: center;"><br /></div>We can observer compilation error with message marked in red rectangle. Why does this happen? <div><ul style="text-align: left;"><li>Maven uses "maven-compiler-plugin" to do the job, even if we don't have this plugin explicitly written into pom.xml</li><li>this plugin (3.1.0) by default tries to set source and target bytecode version to 1.5 even if we set our project to use JDK 11:</li></ul><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjSA4LYoKZdjintGoxltra462I2hB6k5wRPwfZKKPD2sjQamT4twh9eR_0hDy4HISQyrcixZ6N73veQTTDbQUWfhSRi16xpM4zfuTQzxvaRi6SaCkIU1xz4Vcq5Okg35_8cg85itwRM6mg_/s1039/03.png" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="700" data-original-width="1039" height="270" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjSA4LYoKZdjintGoxltra462I2hB6k5wRPwfZKKPD2sjQamT4twh9eR_0hDy4HISQyrcixZ6N73veQTTDbQUWfhSRi16xpM4zfuTQzxvaRi6SaCkIU1xz4Vcq5Okg35_8cg85itwRM6mg_/w400-h270/03.png" width="400" /></a></div><br /><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEizjyQq2Cl5tenR4Q4oLHpQngnsRZZp8xYbXbowf7XHTw2_lsOXHb8fYSqwqcapDMg6UqMDlbfbMPHmUHT3696isgC9DNjWxuyUkf7oAC2PK9IdD3JvEBfGr74JY0kYAmw9_tu4_gC3Qdac/s1041/04.png" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="701" data-original-width="1041" height="269" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEizjyQq2Cl5tenR4Q4oLHpQngnsRZZp8xYbXbowf7XHTw2_lsOXHb8fYSqwqcapDMg6UqMDlbfbMPHmUHT3696isgC9DNjWxuyUkf7oAC2PK9IdD3JvEBfGr74JY0kYAmw9_tu4_gC3Qdac/w400-h269/04.png" width="400" /></a></div><br /><div><br /></div>Solution: </div><div><ul style="text-align: left;"><li>change the plugin version to a newer one - just add the plugin into the pom.xml explicitly</li><li>set the source and target to 11 in the pom.xml. </li></ul><div>Why explicitly set version to 11? Because without explicitly setting them, newer plugin (from 3.6.0) will use 1.6 as default. Compilation is OK now but language level is set to 1.6 even we can use 11 as our JDK:</div></div><div><br /></div><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiISJxl9hyphenhyphenKDbAs_dS-TeRY_8LDbRULMsI3kcp6RagfMe_pAhv-xkkHtVVsVeZcXXuVaeg7JQlMmqEW_lKPG8XG-tU41PsIexbppeHva8x8WpgDt10ToZznGb2fFwDcZCJWwTOz7s0kzLJ5/s2551/05.png" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="977" data-original-width="2551" height="154" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiISJxl9hyphenhyphenKDbAs_dS-TeRY_8LDbRULMsI3kcp6RagfMe_pAhv-xkkHtVVsVeZcXXuVaeg7JQlMmqEW_lKPG8XG-tU41PsIexbppeHva8x8WpgDt10ToZznGb2fFwDcZCJWwTOz7s0kzLJ5/w400-h154/05.png" width="400" /></a></div><br /><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjkEBFr1_-rdtT_Troo1LW6og4CcBRN1UvupgGg9XpjsbPnitidFU8UYLk9g8cqhyHdJOEgctXMssDL_n7CbRfGSS8HQW6MFhg5nG61XF4v5J8_MVrN-eR-TypVXkd7SSYZ3z7r5Fg-DExs/s1041/06.png" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="701" data-original-width="1041" height="215" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjkEBFr1_-rdtT_Troo1LW6og4CcBRN1UvupgGg9XpjsbPnitidFU8UYLk9g8cqhyHdJOEgctXMssDL_n7CbRfGSS8HQW6MFhg5nG61XF4v5J8_MVrN-eR-TypVXkd7SSYZ3z7r5Fg-DExs/s320/06.png" width="320" /></a></div><br /><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEinPryWAwzKntFbeJ0YDj59RK_ZjJZk1rYuX5QLb4NXyVDMKNu30tvrvTyQBS_Xqc0rmfw-VZfBXnqGWsyF0IUriiC1AsvmN-w3vpGT_xQf7Bc47Bqsi6FvRzJepmUR0Np1LOr2d3qMduVK/s1041/07.png" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="701" data-original-width="1041" height="215" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEinPryWAwzKntFbeJ0YDj59RK_ZjJZk1rYuX5QLb4NXyVDMKNu30tvrvTyQBS_Xqc0rmfw-VZfBXnqGWsyF0IUriiC1AsvmN-w3vpGT_xQf7Bc47Bqsi6FvRzJepmUR0Np1LOr2d3qMduVK/s320/07.png" width="320" /></a></div><br /><div><br /></div><div>So, to set up everything together (plugin and language) just put those in pom.xml: </div><pre class="prettyprint"><div><div> <properties></div><div> <maven.compiler.source>11</maven.compiler.source></div><div> <maven.compiler.target>11</maven.compiler.target></div><div> </properties></div><div><br /></div><div> <build></div><div> <pluginManagement></div><div> <plugins></div><div> <plugin></div><div> <groupId>org.apache.maven.plugins</groupId></div><div> <artifactId>maven-compiler-plugin</artifactId></div><div> <version>3.8.1</version></div><div> </plugin></div><div> </plugins></div><div> </pluginManagement></div><div> </build></div></div><div><br /></div></pre>You can also put information about source and target language directly into the plugin instead of defining properties:<div><br /></div><pre class="prettyprint"><div> <build></div><div> <pluginManagement></div><div> <plugins></div><div> <plugin></div><div> <groupId>org.apache.maven.plugins</groupId></div><div> <artifactId>maven-compiler-plugin</artifactId></div><div> <version>3.8.1</version></div><div> <configuration></div><div> <source>11</source></div><div> <target>11</target></div><div> </configuration></div><div> </plugin></div><div> </plugins></div><div> </pluginManagement></div><div> </build></div></pre>From JDK 1.9 and above plugin can also be configured using <release> option:<div><br /></div><pre class="prettyprint"><div><div> <build></div><div> <pluginManagement></div><div> <plugins></div><div> <plugin></div><div> <groupId>org.apache.maven.plugins</groupId></div><div> <artifactId>maven-compiler-plugin</artifactId></div><div> <version>3.8.1</version></div><div> <configuration></div><div> <release>11</release></div><div> </configuration></div><div> </plugin></div><div> </plugins></div><div> </pluginManagement></div><div> </build></div></div></pre>Fortunately, when you create a new maven-based project from scratch in Idea, it (based on the selected JDK) automatically places the appropriate source and target for the language level in pom.xml (using <properties>).Paweł Nieściorukhttp://www.blogger.com/profile/12640446237233312925noreply@blogger.comtag:blogger.com,1999:blog-160318223199333541.post-67560317990958680942021-08-10T11:37:00.000+02:002021-08-10T11:37:09.252+02:00Chargers, cables, standards - everything matters<h2 style="text-align: left;"> The story:</h2><p>Some time ago I tried to charge my powerbank (20000 mAh). I just grab "no-name" charger and some "no-name" cable and start to charge. I took all night to charge it from 0 to 100%. I though this is normal, big powerbank, long time. Later, I tried to charge the same powerbank from 0 to 100% using the same "no-name" charger but with better (let's say "branded") cable. And the charging time was significantly better. I decided to use "branded" charger and "branded" cable and charging from 0 to 100% again. And it was faster than in previous two trials. So I started to read about charging standards, how cable can affect the process and so on. </p><h2 style="text-align: left;">The theory:</h2><p>We have "USB standards" and "prioprietary specifications" in terms of charging. </p><p>USB standards are:</p><p><b>1. Standard USB port i.e. in PC</b></p><p>When connected and transferring data we can charge devices with different power depend on USB type:</p><p></p><ul style="text-align: left;"><li>USB 1.0 gives 0.1A with 5V which gives 0.5W</li><li>USB 2.0 gives 0.5A with 5.0V which gives 2.5W (from 2000)</li><li>USB 3.0 gives 0.9A with 5.0V which gives 4.5W (from 2008)</li><li>USB Type-C gives 1.5A/3.0A with 5.0V which gives 7.5/15.0W (from 2014)</li></ul><p></p><p><b>2. Battery Charging (BC) 1.1/1.2</b></p><p>In year 2000 and up USB 2.0 with its 2.5W was not enough. In 2007 a Battery Charging 1.1 (BC 1.1) specification was released. It defined three new types of ports:</p><p></p><ul style="text-align: left;"><li>Standard Downstream Port (SDP) - USB 2.0 compatible in terms of power (2.5W)</li><li>Dedicated Charging Port (DCP) - does not support data transfer, used only for charging. Support max 1.5A with 5.0V which gives max 7.5W</li><li>Downstream Port (CDP) - allows both data transfer like USB 2.0 and charging like DCP</li></ul><p></p><p>In 2010 a Battery Charging 1.2 (BC 1.2) specification was released, where 1.5A was increased to 5A, which gives max 25W using 5.0V. </p><p><b>3. Power Delivery (PD) 1.0/2.0/3.0</b></p><p>In 2012 Power Delivery 1.0 specification was released. It introduces so called source profiles:</p><p></p><ul style="text-align: left;"><li>5.0V with 2.0A which gives 10W (profile 1)</li><li>12.0V with 1.5A which gives 18 W (profile 2)</li><li>12.0V with 3.0A which gives 36 W (profile 3)</li><li>20.0V with 3.0A which gives 60W (profile 4)</li><li>20.0V with 5.0A which gives 100W (profile 5) </li></ul><p></p><p>In 2014 Power Delivery 2.0 specification was released (as a part of USB 3.1 and USB Type-C). Comparing to PD 1.0, 12V is gone and 9V and 15V are introduced. Source profiles no longer exist - they were replaced by power rules. Power rules keep fixed voltage but allow to negotiate current level, which gives different power level: </p><p></p><ul style="text-align: left;"><li>5.0V with 0.1A - 3.0A which gives 0.5W - 15W (rule 1)</li><li>9.0V with 1.67A - 3.0A which gives 15W - 27W (rule 2)</li><li>15.0V with 1.8A - 3.0A which gives 27W - 45W (rule 3)</li><li>20.0V with 2.25A - 3.0A / 3.0A - 5.0A (using rated cable) - 45W - 100W (rule 4)</li></ul><p></p><p>In 2018 Power Delivery 3.0 specification was released. It does not change power rules introduced in PD 2.0 but adds some features like Programmable Power Supply (PPS) which allows configure voltage in 20mV granular increments.</p><p><b>4. Prioprietary specifications</b></p><p></p><ul style="text-align: left;"><li>Qualcomm Quick Charge (QC 1.0/2.0/3.0/4.0/4+)</li><li>MediaTek Pump Express</li><li>Samsung Adaptive Fast Charging</li><li>Oppo VOOC (named Dash Charge/Warp Charge on OnePlus devices and Dart Charge on Realme devices)</li><li>Huawei SuperCharge</li></ul><p></p><h2 style="text-align: left;">The test:</h2><p>Lets have a look how it looks like in real life. I will charge those devices:</p><p></p><ul style="text-align: left;"><li>Tablet Lenovo Tab2 A10-70L</li><li>Xiaomi YDDYP01 20000mAh power bank</li><li>Xiaomi Redmi 5 Plus smartphone</li><li>Xiaomi POCO X3 NFC smartphone</li><li>Aukey PB-XD26 26800mAh power bank</li><li>Sony Xperia 10 III smartphone</li></ul><p></p><p>I will use chargers:</p><p></p><ul style="text-align: left;"><li>Aukey PT-Y11 wall charger which have to ports: USB-A supports Quick Charge 3.0 (18W) and USB-C supports Power Delivery 3.0 (30W). </li><li>Xiaomi POCO X3 stock charger delivered with the phone.</li><li>Sony Xperia 10 III stock charger delivered with the phone.</li></ul><p></p><p>I will use cables: </p><p></p><ul style="text-align: left;"><li>1m Aukey USB-A/micro-USB</li><li>1m Aukey USB-A/USB-C</li><li>1m Aukey USB-C/USB-C (rated for 100W for PD 3.0)</li></ul><p></p><p>I will use testers:</p><p></p><ul style="text-align: left;"><li>Fnirsi FNB28 for USB-A</li><li>Fnirsi FNC88 for USB-C</li></ul><p></p><h2 style="text-align: left;">Results:</h2><p><b>1. Lenovo Tab2 A10-70L</b> </p><p>Connection: Aukey PT-Y11 USB-A -> Fnirsi FNB28 -> USB-A/micro-USB -> Lenovo Tab2 A10-70L</p><p>Power measured: 10W</p><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjw8efnLhtok42C8LkzBtXfYSg6fJPTaIR6nMcJKtTnwyxLjKlp06ZeRnsvXE9AVXYvfv8b9u-d-OeBvFl44Hix-4JQ3s3oReLovabHvPtJ-w6bSvHETCdByJBJl9xv4to1c35fz37y5vK0/s4000/01.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="4000" data-original-width="3000" height="400" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjw8efnLhtok42C8LkzBtXfYSg6fJPTaIR6nMcJKtTnwyxLjKlp06ZeRnsvXE9AVXYvfv8b9u-d-OeBvFl44Hix-4JQ3s3oReLovabHvPtJ-w6bSvHETCdByJBJl9xv4to1c35fz37y5vK0/w300-h400/01.jpg" width="300" /></a></div><br /><p>I cannot find if it is compatible with any proprietary specification, but 10W suggests it is Qualcomm QC 1.0. Tester does not recognize it as Qualcomm QC 1.0, intstead it showh Batery Charge 1.1 DCP standard.</p><p><b>2. Xiaomi YDDYP01 20000mAh</b></p><p>Connection: Aukey PT-Y11 USB-A -> Fnirsi FNB28 -> USB-A/micro-USB -> Xiaomi YDDYP01 20000mAh. </p><p>Power measured: 18W</p><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhauAtCAYO_i8nCCPtJHVkvmp1TyMyDtmueKiQBwkFY3DuHdtQkT7n50CsuxQpWwC-ojuQXQo7GV8lxg88jXeSa5sp1hF-_MllzNmSBP6zPtgEsZ0RmyCJT5Y7C9JNqMFb5YGNjb6I4kmKw/s4000/02.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="4000" data-original-width="3000" height="400" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhauAtCAYO_i8nCCPtJHVkvmp1TyMyDtmueKiQBwkFY3DuHdtQkT7n50CsuxQpWwC-ojuQXQo7GV8lxg88jXeSa5sp1hF-_MllzNmSBP6zPtgEsZ0RmyCJT5Y7C9JNqMFb5YGNjb6I4kmKw/w300-h400/02.jpg" width="300" /></a></div><br /><p>Accroding to this device specification it is compatible with Qualcomm QC 2.0 and tester recognizes it indeed as Qualcomm QC 2.0 12V</p><p><b>3. Xiaomi Redmi 5 Plus (smartphone was initially charged about 30% during the test) </b></p><p>Connection: Aukey PT-Y11 USB-A -> Fnirsi FNB28 -> USB-A/micro-USB -> Xiaomi Redmi 5 Plus</p><p>Power measured: 14W</p><p></p><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjXqa0_pq-Qj-MP9nu9IhsSMYN_LVFjhAWI74O0nP6DeJHspUAa7neBhJ8HZhytPnviFdh2aa96t-hzebr4Ut33E5hfkkHZYeYLAKycq36OOpNQ8yr14PUlKkXaypFtGobZpL8P-pAv7GHR/s4000/03.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="4000" data-original-width="3000" height="400" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjXqa0_pq-Qj-MP9nu9IhsSMYN_LVFjhAWI74O0nP6DeJHspUAa7neBhJ8HZhytPnviFdh2aa96t-hzebr4Ut33E5hfkkHZYeYLAKycq36OOpNQ8yr14PUlKkXaypFtGobZpL8P-pAv7GHR/w300-h400/03.jpg" width="300" /></a></div><br /> The device is based on Qualcomm Snapdragon 625 which is in theory Qualcomm QC 3.0 compatible (according to specification of this chipset). However tester recognizes it as Qualcomm Quick Charge 2.0 9V, so I suspect that smartphone manufacturer "downgraded" charging to QC 2.0 in order not to pay higher license fo QC 3.0,<p></p><p><b>4. Xiaomi POCO X3 NFC (smartphone was initially charged about 85% during each the test) </b></p><p><b>Test 1:</b></p><p>Connection: Aukey PT-Y11 USB-A -> Fnirsi FNB28 -> USB-A/USB-C -> Xiaomi POCO X3 NFC</p><p>Power measured: 16W</p><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEing9vk4hSiA9MjDUbNpo5xmVQ-tUellJO9vRqFWSEzKwaJ7wHGzcwqQaYcEBftzIK1ERiUmKDtnsEAkCx1H9v45DJMgSHW5v2mOyMFxpTlvrlBlftnn6eoeMqGfB-a9GipCTAeshpp4iJp/s4000/04.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="4000" data-original-width="3000" height="400" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEing9vk4hSiA9MjDUbNpo5xmVQ-tUellJO9vRqFWSEzKwaJ7wHGzcwqQaYcEBftzIK1ERiUmKDtnsEAkCx1H9v45DJMgSHW5v2mOyMFxpTlvrlBlftnn6eoeMqGfB-a9GipCTAeshpp4iJp/w300-h400/04.jpg" width="300" /></a></div><br /><p>The device is based on Qualcomm Snapdragon 732G which is in theory Qualcomm QC 4+ compatible (according to specification of this chipset). Tester recognizes it as Qualcomm QC 3.0 which is OK because charger is QC 3.0 compatible and QC 4+ device is backward compatible with QC 3.0</p><p><b>Test 2:</b></p><p>Connection: Aukey PT-Y11 USB-C -> Fnirsi FNC88 -> USB-C/USB-C -> Xiaomi POCO X3 NFC</p><p>Power measured: 14W</p><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhJ6Zb6m6aKgKOukpweZki7SDORDlcmbnw6drMtyOkd6EeELWunoYf4cWohDM7Ro3ICfxY8veLn4pqtgKE1ByLghr_jF9SvPENxpRQrlVoxXvpGitrGXpyt5bs5DJWFs7sm5cfFoaa0W9d4/s4000/05.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="4000" data-original-width="3000" height="400" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhJ6Zb6m6aKgKOukpweZki7SDORDlcmbnw6drMtyOkd6EeELWunoYf4cWohDM7Ro3ICfxY8veLn4pqtgKE1ByLghr_jF9SvPENxpRQrlVoxXvpGitrGXpyt5bs5DJWFs7sm5cfFoaa0W9d4/w300-h400/05.jpg" width="300" /></a></div><p>QC 4+ is compatible with Power Delivery (and can give max 27W), so charging with PD port of this charger simply works. However tester recognizes it as Huawei SCP standard.</p><p><b>Test 3:</b></p><p>Connection: Xiaomi stock charger USB-C -> Fnirsi FNB28 -> USB-A/USB-C -> Xiaomi POCO X3 NFC</p><p>Power measured: 20W</p><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhjphNTktYkWtFJcW3XSI7t6qDUgdiWB_ZDrzw2DR4eR-U0Nsc6V-XkotKXiWoZoCQHTiDsYpgElkZN5mNISM4QemV7x7wP0TIKIyRQKn20AgVz11iQcOp6c1x6QnGTs6koQEL0H_oYfbZM/s4000/06.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="4000" data-original-width="3000" height="400" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhjphNTktYkWtFJcW3XSI7t6qDUgdiWB_ZDrzw2DR4eR-U0Nsc6V-XkotKXiWoZoCQHTiDsYpgElkZN5mNISM4QemV7x7wP0TIKIyRQKn20AgVz11iQcOp6c1x6QnGTs6koQEL0H_oYfbZM/w300-h400/06.jpg" width="300" /></a></div><br /><p>Xiaomi stock charger has max 27W, so it is QC 4.0 compatible, however tester recognizes it as QC 3.0.</p><p><b>5. Aukey PB-XD26 26800mAh </b></p><p>Connection: Aukey PT-Y11 USB-C -> Fnirsi FNC88 -> USB-C/USB-C -> Aukey PB-XD26 26800mAh</p><p>Power measured: 30W</p><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh5CvPuKlnxqhL6SORL74my7QNVFMaTk03E_hh6qoY7xDipesAIlnf-_15-sstqnVgGXOViHfIfybudSddeccbrIAOTaWE-VML9PZxD4PdX9UT6CmANvVagH4LEaJgvNKu-AqDVC5kvLwxr/s4000/07.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="4000" data-original-width="3000" height="400" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh5CvPuKlnxqhL6SORL74my7QNVFMaTk03E_hh6qoY7xDipesAIlnf-_15-sstqnVgGXOViHfIfybudSddeccbrIAOTaWE-VML9PZxD4PdX9UT6CmANvVagH4LEaJgvNKu-AqDVC5kvLwxr/w300-h400/07.jpg" width="300" /></a></div><br /><p>According to specification of this power bank it is PD 3.0 compatible and its max charging power is 45W. Used charger has max 30W output via its PD port, so charging works with its maximum speed due to charger limit (limited to 30W by charger). However tester does not recognize it as Power Delivery standard.</p><p><b>6. Sony Xperia 10 III</b></p><p><b>Test 1:</b></p><p>Connection: Aukey PT-Y11 USB-A -> Fnirsi FNB28 -> USB-A/USB-C -> Sony Xperia 10 III</p><p>Power measured: 7W</p><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi02zSFiNJ2pnH5qll_FZPzEsG0WeWYWKSCrDP9-85SFTSk8KVaSS6E5voOiyx7jozbDGJbZq4ywkBNoqTK_LCbajMtLCiLqUrC47pUO_CyilspSH-5WmTI_Q4VZo1ViJE5Gwdwi8PdeeGf/s4624/08.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="4624" data-original-width="3472" height="400" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi02zSFiNJ2pnH5qll_FZPzEsG0WeWYWKSCrDP9-85SFTSk8KVaSS6E5voOiyx7jozbDGJbZq4ywkBNoqTK_LCbajMtLCiLqUrC47pUO_CyilspSH-5WmTI_Q4VZo1ViJE5Gwdwi8PdeeGf/w300-h400/08.jpg" width="300" /></a></div><br /><p>The device is based on Qualcomm Snapdragon 690 5G which is in theory Qualcomm QC 4+ compatible (according to specification of this chipset). But this phone does not support any QC standard (probably due to licensing costs). Moreover stock charger delivered with the phone gives also max 7,5W power.</p><p><b>Test 2:</b></p><p>Connection: Aukey PT-Y11 USB-C -> Fnirsi FNC88 -> USB-C/USB-C -> Sony Xperia 10 III</p><p>Power measured: 15W</p><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi_wXrVvaXenX0kxGJecn8LhIDdJCaTbTswBBz7vYrGzzUaRHOoYzasQjqPdk_o2Uo3Z3LS2iTJqXTG7GGkj-jSG12V2BtAN5WVFNF8VzjQX5iufkZB2eBxDZ0dL36QhNDK5iPxr5TVH_rr/s4624/09.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="4624" data-original-width="3472" height="400" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi_wXrVvaXenX0kxGJecn8LhIDdJCaTbTswBBz7vYrGzzUaRHOoYzasQjqPdk_o2Uo3Z3LS2iTJqXTG7GGkj-jSG12V2BtAN5WVFNF8VzjQX5iufkZB2eBxDZ0dL36QhNDK5iPxr5TVH_rr/w300-h400/09.jpg" width="300" /></a></div><br /><p>In theory this Sony phone can take 30W via PD. I have never achieved such value, I was able to get 18W max.</p><h2 style="text-align: left;">Final notes:</h2><p></p><ol style="text-align: left;"><li>The simple truth is: the more power (W) delivered, the faster charging.</li><li>Charged device and charger should be compatible with the same charge standard.</li><li>USB cable used for charging must have good quality too. </li><li>Those USB testers used are not always reliable in my opinion:</li></ol><ul style="text-align: left;"><ul><li>they do not properly recognize charging standards (especially PD, QC 4.0)</li><li>sometimes I had to connect them second or third time to start charging i.e 18W not 10W. It looks like sometimes "handshake" between charged device and charger with tester plugged in between does not work (or works depending on the connection order...)</li></ul></ul><p></p><p><br /></p>Paweł Nieściorukhttp://www.blogger.com/profile/12640446237233312925noreply@blogger.comtag:blogger.com,1999:blog-160318223199333541.post-52336776543969669462019-01-13T18:18:00.009+01:002023-06-11T07:58:02.550+02:00Gather all hammers inside toolbox - revisitedIt's been a long time since I wrote something here. When I started this blog, one of the first thing I wrote about was how to set up development environment. It was ten years ago - see <a href="https://technology-for-human.blogspot.com/2009/04/zbieramy-motki-do-skrzynki-narzedziowej.html">here</a>. A lot of things changed in Java world in this time. Let's try to set up everything again using modern and most popular tools now.<br />
<div>
<br /></div>
<div>
What are we going to do?</div><div><ol style="text-align: left;"><li>Install Java JDK - we still have Java like in the past, however significant changes were done by Oracle last time.</li><li>Install IntelliJ Idea - an IDE, as replacement for Eclipse.</li><li>Install Git as version control system for our source code.</li><li>Write a sample Java "Hello World" application to check if installed IDE and Java works.</li><li>Use Git with our "Hello World" application.</li><li>Install Apache Maven for dependency management in our projects.</li></ol></div><div>
<div>
As before, all things will be installed in <span style="font-family: Courier New, Courier, monospace;">C:\Development</span></div></div><div><span style="font-family: Courier New, Courier, monospace;"><br /></span></div>
<h2 style="text-align: left;">Installing Java</h2><div><br /></div><div><div>We all get used to that Java is free to use either for personal or commercial purpose. It was true for Java 8 and older versions until 1st January 2019. Starting from 1st Jan 2019 Oracle announced end of public updates of Java SE 8. It simply means If You use Java in commercial purpose and You want get updates for Your Java SE 8, from 1st January 2019 You have to pay for this.<br />
<br /></div>
<div>
Why Java 8, what about other versions like 9,10,11...? From Java 8, every three years Oracle will relese Java version known as Long Time Support (LTS). First LTS is Java 8, second is Java 11 - it means that Java 9 and 10 is no longer supported now. Changing licensing model for commercial use means that You have to pay for the updates/support for each LTS version starting from 8 (from 1st Jan 2019). See more <a href="https://www.oracle.com/technetwork/java/java-se-support-roadmap.html">here</a>.<br />
<br /></div>
<div>
So what are options? We can:</div>
<div>
1. Stay with latest available Java 8 from Oracle (JDK 8u192) without possibility to update anything - we don't have security updates, but we are free of charge.</div>
<div>
2. Become Java Commerical User and pay for latest JDK (11 at the time of writing)</div>
<div>
3. Use OpenJDK from Oracle - this is free Java for commercial use, but each new version is publicly available in 6-month interval, with 6-month support/updates available. So every half a year You have to update used OpenJDK on Your own.</div><div>4. Use a Java based on OpenJDK but with long-term support - for example Amazon Corretto 11 available <a href="https://docs.aws.amazon.com/corretto/latest/corretto-11-ug/what-is-corretto-11.html" target="_blank">here</a>.</div><div>
<br />
Let's use JDK 8 from Oracle from point 1. If You want to use latest free Java with LTS You can use Amazon Corretto from point 4 - just download .zip file and extract it for exmple into <span style="font-family: courier;">C:\Development\Java\AmazonJDK\Java11_64bit</span></div>
<div>
<br /></div>
<div>
<b>Step 1:</b> Download 64 bit version of JDK 8u192 or Windows from <a href="https://www.oracle.com/technetwork/java/javase/downloads/index.html">here</a>.<br />
<b>Step 2: </b>Create directory <span style="font-family: Courier New, Courier, monospace;">C:\Development\Java\OracleJDK\Java8_64bit</span> and start the installer:<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiHU2vJn118oNZXBeWpGcDOniMTh1-exT8pn49_0NYbBrMIPmbVjPN_bntty2FhNhQcQAimgK_CvUyNFpKLtP5yIFz5u8hc9GiYli7xE-tt_rbkK2mK0L5kinpHAjA5qIoGmG2b8SObpDT9/s1600/2019.01.06_1.png" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="592" data-original-width="781" height="302" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiHU2vJn118oNZXBeWpGcDOniMTh1-exT8pn49_0NYbBrMIPmbVjPN_bntty2FhNhQcQAimgK_CvUyNFpKLtP5yIFz5u8hc9GiYli7xE-tt_rbkK2mK0L5kinpHAjA5qIoGmG2b8SObpDT9/s400/2019.01.06_1.png" width="400" /></a></div>
<br />
Remove installing public JRE and choose previously created directory:<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjT1tMd_146CEroKbf8UGZ2I5wnHn7wcYAvs29KqNJ_kfI4rjoLgOhlxEo0g9nuKZbi-G2bl5D1x9FkfMSPek_JQpgoSIM5FZBU7WpKpcVzH3QhYvKy6A5n-Eg7uEakQ8Im0k-vqsq_Ybb-/s1600/2019.01.06_2.png" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="592" data-original-width="781" height="302" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjT1tMd_146CEroKbf8UGZ2I5wnHn7wcYAvs29KqNJ_kfI4rjoLgOhlxEo0g9nuKZbi-G2bl5D1x9FkfMSPek_JQpgoSIM5FZBU7WpKpcVzH3QhYvKy6A5n-Eg7uEakQ8Im0k-vqsq_Ybb-/s400/2019.01.06_2.png" width="400" /></a></div>
<br />
<b>Step 3:</b> After succesfull install create system variable named <span style="font-family: Courier New, Courier, monospace;">JAVA_HOME</span> with value <span style="font-family: Courier New, Courier, monospace;">C:\Development\Java\OracleJDK\Java8_64bit </span><span style="font-family: inherit;">then modify </span><span style="font-family: Courier New, Courier, monospace;">Path</span><span style="font-family: inherit;"> system variable by adding </span><span style="font-family: Courier New, Courier, monospace;">%JAVA_HOME%\bin</span> at the end.<br />
<br />
<b>Step 4:</b> Verify Java installation by opening command line and type <span style="font-family: Courier New, Courier, monospace;">java -version</span> and j<span style="font-family: Courier New, Courier, monospace;">avac -version</span>. You should see informationa about Java installed:<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh6GsI4dsX0bYkXTOGbiJtB_m8JJrvncrfJuSoozquZj6B-wyihwV2_y3QEGs8ffECxTf_JhmwiXREdvWhFfFkVMehEU-SjCdfsaLPuuss179YGk5NEWyfLrCnYDv6Tx0oUdFj_lGwOKESO/s1600/2019.01.06_2_1.png" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="394" data-original-width="930" height="168" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh6GsI4dsX0bYkXTOGbiJtB_m8JJrvncrfJuSoozquZj6B-wyihwV2_y3QEGs8ffECxTf_JhmwiXREdvWhFfFkVMehEU-SjCdfsaLPuuss179YGk5NEWyfLrCnYDv6Tx0oUdFj_lGwOKESO/s400/2019.01.06_2_1.png" width="400" /></a></div>
<br />
Java is installed.<br /><br /><h2 style="text-align: left;">Installing IntelliJ Idea</h2><div><br /></div><b>Step 1: </b>Create directory <span style="font-family: Courier New, Courier, monospace;">C:\Development\Jetbrains\Idea</span><br />
<div>
<b>Step 2:</b> Download and install an application JetBrains Toolbox from <a href="https://www.jetbrains.com/toolbox/">here</a>. This is an application to maintain all products from Jetbrains (IntelliJ Idea manufacturer). It allows to automatically update installed tools.<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhZAYEdyKZ8-VoxkvGxV0nbfnKp74M3cdekNIlOhTmkNN2UEEXT528Kw0aq_IsQfVQZrtdga2XCz8U-uArEbOynsD2odX-GhUl_7DTbG_Zj_GpkFLfSsBvlGGDkgf7IXd_45JoQU_PRyuZR/s1600/2019.01.06_3.png" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="565" data-original-width="748" height="301" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhZAYEdyKZ8-VoxkvGxV0nbfnKp74M3cdekNIlOhTmkNN2UEEXT528Kw0aq_IsQfVQZrtdga2XCz8U-uArEbOynsD2odX-GhUl_7DTbG_Zj_GpkFLfSsBvlGGDkgf7IXd_45JoQU_PRyuZR/s400/2019.01.06_3.png" width="400" /></a></div>
<br />
<b>Step 3: </b>Configure toolbox.<br />
<br />
Choose created directory in step 1. Do not forget to click apply after choosing it. If You have any license from Jetbrains, log in into your account now - You will not have to provide license when installing tools:<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEimqxEDKbw0KiEr7A2AA74JtiJh_ADg5MTc0U9IQ6f1CTaAhaPxokBakfdZWNXb_bot58CGPJGK-H6p5tyu7_U7ryAmnFAFeszO4i6dmN4MnWcsT8_Ce3PkZhqDA-98SgcGIGIEaMjayIr4/s1600/2019.01.06_4.png" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="974" data-original-width="661" height="400" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEimqxEDKbw0KiEr7A2AA74JtiJh_ADg5MTc0U9IQ6f1CTaAhaPxokBakfdZWNXb_bot58CGPJGK-H6p5tyu7_U7ryAmnFAFeszO4i6dmN4MnWcsT8_Ce3PkZhqDA-98SgcGIGIEaMjayIr4/s400/2019.01.06_4.png" width="271" /></a></div>
<br />
Now You can choose a tool to install:<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjEGvVrSjPMH86y1RzRwyN3WonxTGAiOdodV6F-_lLnDzNUFk1xbg1tudiR0nmqB3dcsR87dWHjC6AMv3qma7bEmOh2Y8R4meqRWRerEp2yf9BUbKpT4X5X6_fcGCXgLouESD0pLM-l6QDu/s1600/2019.01.06_5.png" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="974" data-original-width="661" height="400" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjEGvVrSjPMH86y1RzRwyN3WonxTGAiOdodV6F-_lLnDzNUFk1xbg1tudiR0nmqB3dcsR87dWHjC6AMv3qma7bEmOh2Y8R4meqRWRerEp2yf9BUbKpT4X5X6_fcGCXgLouESD0pLM-l6QDu/s400/2019.01.06_5.png" width="271" /></a></div>
<br />
After click on a tool, it will be installed into the previously selected directory:<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgGIv0JbQYCCSm_JOYa54-tW2aPbz6n-avZJWlPAtCdiKSh9i1-F2n_a5IsdZP4xBW2_of0TQ4NAkF-RuvpPTy1qbl0LMzVsKFRud2WV4WZo6XA1z83rFcB3ShBo9RGM69U_9xCT2peTrxN/s1600/2019.01.06_6.png" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="974" data-original-width="661" height="400" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgGIv0JbQYCCSm_JOYa54-tW2aPbz6n-avZJWlPAtCdiKSh9i1-F2n_a5IsdZP4xBW2_of0TQ4NAkF-RuvpPTy1qbl0LMzVsKFRud2WV4WZo6XA1z83rFcB3ShBo9RGM69U_9xCT2peTrxN/s400/2019.01.06_6.png" width="271" /></a></div>
<br />
Just click on tool to launch it. You can also change settings of the tool being installed, like update plan, memory consumption and so on:<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjJHLTNRgnvdluTMV2iemLkbCiemoWEw2GYEWcojXAfUAl2ZT57lMqQMjUiApELKJLXuCGeNJK1yQOr0pIYBIUSmylz4EWuvnOGJxml0NEvZSyBdAdb18IR9yDhRDHUAZz6CefQWyoiSk8H/s1600/2019.01.06_7.png" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="974" data-original-width="661" height="400" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjJHLTNRgnvdluTMV2iemLkbCiemoWEw2GYEWcojXAfUAl2ZT57lMqQMjUiApELKJLXuCGeNJK1yQOr0pIYBIUSmylz4EWuvnOGJxml0NEvZSyBdAdb18IR9yDhRDHUAZz6CefQWyoiSk8H/s400/2019.01.06_7.png" width="271" /></a></div>
<br />
IntelliJ Idea is installed. Start it, then go thru some creator in order to perform basic settings (You may leave all the options default).</div><div><br /></div><div><h2 style="text-align: left;">Installing Git</h2><div><b><br /></b></div><div>Git will be used to track changes we make in our projects files. </div><div><br /></div><div><b>Step 1:</b> Download Git for Windows from <a href="https://git-scm.com/download/win">here</a>. </div><div><b>Step 2:</b> Create directory <span style="font-family: Courier New, Courier, monospace;">C:\Development\Git</span>. Run the downloaded git installer and choose created directory as the installation target: </div><div><br /></div><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg4uj7x5twgwFqOY2oOTF27tSnQolhO0jwf96lY7_PUXqmg-Fw_veR4bxhWQaZJ9XKZpz8W5Z-3J90LiIfOIIjFlO0eVp0FQMcHubMz_ZrrBNqOLXx1RF4IdqIuBjZjJ3Nj6m3Mgk5_BBgo/s1600/2019.01.06_19.png" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="560" data-original-width="744" height="300" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg4uj7x5twgwFqOY2oOTF27tSnQolhO0jwf96lY7_PUXqmg-Fw_veR4bxhWQaZJ9XKZpz8W5Z-3J90LiIfOIIjFlO0eVp0FQMcHubMz_ZrrBNqOLXx1RF4IdqIuBjZjJ3Nj6m3Mgk5_BBgo/s400/2019.01.06_19.png" width="400" /></a></div><div><br /></div><div>Install Git using all defaults (including vim as default editor), but allow git to be used from command line:</div><div><br /></div><div class="separator" style="clear: both; text-align: center;"><br /></div><br /><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgA8pkLZ7qP-EPNG2XLVmnn0j5HOJ6CH518HLiTbORihGeB03QWqKMHeZYolmBkVEUK5P6n-1XEZgNP32MHMgjcD8g9Wijh2RRzu7eZC0BzQbaLWXgPOH0eAWYKX7whX78pOsq5bVC1Dasf/s1600/2019.01.06_21.png" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="564" data-original-width="748" height="301" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgA8pkLZ7qP-EPNG2XLVmnn0j5HOJ6CH518HLiTbORihGeB03QWqKMHeZYolmBkVEUK5P6n-1XEZgNP32MHMgjcD8g9Wijh2RRzu7eZC0BzQbaLWXgPOH0eAWYKX7whX78pOsq5bVC1Dasf/s400/2019.01.06_21.png" width="400" /></a></div><div class="separator" style="clear: both; text-align: center;"></div><div><br /></div><div><br /></div><div><b>Step 3:</b> Verify installation and basic configuration. </div><div><br /></div><div>Open command prompt and execute command <span style="font-family: Courier New, Courier, monospace;">git -version</span> to see if git responds. After that configure user and email by typing commands (replace XYZ with Your value):</div><div> </div><div><span style="font-family: courier;">git config --global user.name "XYZ"</span></div><div><span style="font-family: courier;">git config --global user.email "XYZ"</span></div><div><span style="font-family: courier;"><br /></span></div><div>Below screen shows all those commands and their results:</div><div><br /></div><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi_yeX0Y0N0UQpN9i3SINjZIUGnueCc4U1WvQ8M916pEPHN8DPxPHbe-JzF8oVD_DwPb4i-2B3AMgA3-a2G4M8rPRUD1ztpIz82u2DopJGTjDGwpPIhEM1dn8g9-GttqImYZrtzb0d59JS8/s1600/2019.01.06_22.png" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="766" data-original-width="1348" height="226" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi_yeX0Y0N0UQpN9i3SINjZIUGnueCc4U1WvQ8M916pEPHN8DPxPHbe-JzF8oVD_DwPb4i-2B3AMgA3-a2G4M8rPRUD1ztpIz82u2DopJGTjDGwpPIhEM1dn8g9-GttqImYZrtzb0d59JS8/s400/2019.01.06_22.png" width="400" /></a></div><br /><b>Step 4:</b> Make IntelliJ aware of installed Git.<br /><br />Open settings and type Git in search box. Go to Git settings and set path to git.exe:<br /><br /><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiynaZp-JiM1RhGgl0DH7slIxgEvqilG8EJlSHKA9KSLkm_6Eb-smbCz_M4Wsbo89AicZlWjgH7arjHiLev5mEThdN12CUqcVancPOt0odeLeZ2_Ao5IcGyA8O0a89vwpqXQyxGYOhMV6G2/s1600/2019.01.06_23.png" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="824" data-original-width="1537" height="213" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiynaZp-JiM1RhGgl0DH7slIxgEvqilG8EJlSHKA9KSLkm_6Eb-smbCz_M4Wsbo89AicZlWjgH7arjHiLev5mEThdN12CUqcVancPOt0odeLeZ2_Ao5IcGyA8O0a89vwpqXQyxGYOhMV6G2/s400/2019.01.06_23.png" width="400" /></a></div><br />That's all. Git is installed and configured in IntelliJ Idea.<br />
<br />
<h2 style="text-align: left;">"Hello World" - sample java application</h2><div><br /></div>
<div>
<b>Step 1:</b> Create directory <span style="font-family: Courier New, Courier, monospace;">C:\Development\Projects</span></div>
<div>
<br /></div>
<div>
<b>Step 2:</b> Run Idea. You will see initial screen for creating/opening/downloading a project. Choose create new project:</div>
<div>
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgoSiUsraxvfNVJtvzwZaRj0SEyc6ySXlticnm4bbHYM-k-rYLA6uxoICbERyJ0a27-XqJFztVoDx-_Z-crA3DcTqHkdPVKDs_V2_O4d1eDR4bN11V8NfxXTW7ctuMfYIb75AZQYWOVTEDZ/s1600/2019.01.06_8.png" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="722" data-original-width="1165" height="247" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgoSiUsraxvfNVJtvzwZaRj0SEyc6ySXlticnm4bbHYM-k-rYLA6uxoICbERyJ0a27-XqJFztVoDx-_Z-crA3DcTqHkdPVKDs_V2_O4d1eDR4bN11V8NfxXTW7ctuMfYIb75AZQYWOVTEDZ/s400/2019.01.06_8.png" width="400" /></a></div>
<div>
<br /></div>
<div>
<b>Step 2:</b> Choose Java project type:</div>
<div>
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjVegyCug1akKeekshnQwAn5XKHbSzls5Y8EI8gRrUF2Ddo1KB3rOM1Q3OXc5URYovEss-bZKLze9QSfRI0qrfpmF-zjF01lDU_3d5qNeRGrW9mCDYICRXAsbjEV-Pf1hk3cCT_SyQyPFSC/s1600/2019.01.06_9.png" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="1008" data-original-width="1052" height="382" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjVegyCug1akKeekshnQwAn5XKHbSzls5Y8EI8gRrUF2Ddo1KB3rOM1Q3OXc5URYovEss-bZKLze9QSfRI0qrfpmF-zjF01lDU_3d5qNeRGrW9mCDYICRXAsbjEV-Pf1hk3cCT_SyQyPFSC/s400/2019.01.06_9.png" width="400" /></a></div>
<div>
<br /></div>
<div>
Idea is shipped with its own JDK. We would like to use our own JDK. Press button <span style="font-family: Courier New, Courier, monospace;">New</span> ans select the JDK directory <span style="font-family: "Courier New", Courier, monospace;">C:\Development\Java\OracleJDK\Java8_64bit </span><span>(or another directory like <span style="font-family: courier;">C:\Development\Java\AmazonJDK\Java11_64bit</span> if You use Amazon Corretto)</span><span style="font-family: "Courier New", Courier, monospace;">. </span><span style="font-family: inherit;">Then press </span><span style="font-family: Courier New, Courier, monospace;">Next</span><span style="font-family: inherit;">.</span></div>
<div>
<br /></div>
<div>
<b>Step 3:</b> Choose project type from template: </div>
<div>
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjDQXAa4i1jqJnEdTJqWr8vBKK-l9azDzMwuFGpMsu-tGs2ufJk_ftd7J71H9oWlZX0gNPrkDH_YKef5a_TOX5U5fDeqJ6IPZhHYY25XQfxq-hhV6QCYuw2Ig7QpSY6ovU-LKusj_fx8IRm/s1600/2019.01.06_10.png" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="605" data-original-width="953" height="253" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjDQXAa4i1jqJnEdTJqWr8vBKK-l9azDzMwuFGpMsu-tGs2ufJk_ftd7J71H9oWlZX0gNPrkDH_YKef5a_TOX5U5fDeqJ6IPZhHYY25XQfxq-hhV6QCYuw2Ig7QpSY6ovU-LKusj_fx8IRm/s400/2019.01.06_10.png" width="400" /></a></div>
<div>
</div>
<div>
<b>Step 4:</b> Choose project name and location (use projects directory created in step 1): </div>
<div>
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiocSeZAbrBS5viwG5POIIhFkkSeeVS9m7vtnyo4W1hn0sAe63Yln0volEkydbT8sX9E8kqYtqn0dQ9517NaImnmg1XObonTwqbGQXFainU4DbFMhGZAgVEFd-3lAqqpoTE8EYczRDae50c/s1600/2019.01.06_11.png" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="704" data-original-width="1021" height="275" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiocSeZAbrBS5viwG5POIIhFkkSeeVS9m7vtnyo4W1hn0sAe63Yln0volEkydbT8sX9E8kqYtqn0dQ9517NaImnmg1XObonTwqbGQXFainU4DbFMhGZAgVEFd-3lAqqpoTE8EYczRDae50c/s400/2019.01.06_11.png" width="400" /></a></div>
<div>
<br /></div>
<div>
<b>Step 5:</b> Sample application is ready. You can start is using green "play" button:</div>
<div>
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhQjkW8M_R4qoX2zSbfylYNyIE1JHitbuR51c6DUos6mJJqREPcrHxO1Dexf7drefedqeqIfRFqrlabflkpFjVRULzC2VtJ1-yZRuA-8Hx9Nk4jC4ijofbkLvNndlwYTlZiZMh-DHDUypj5/s1600/2019.01.06_12.png" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="850" data-original-width="1600" height="170" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhQjkW8M_R4qoX2zSbfylYNyIE1JHitbuR51c6DUos6mJJqREPcrHxO1Dexf7drefedqeqIfRFqrlabflkpFjVRULzC2VtJ1-yZRuA-8Hx9Nk4jC4ijofbkLvNndlwYTlZiZMh-DHDUypj5/s320/2019.01.06_12.png" width="320" /></a></div>
<div>
<br /></div>
<div>
<br /></div>
<div>
<b>Step 6 (optional):</b> Leave only our installed JDK for future projects. </div>
<div>
<br /></div>
<div>
As mentioned above, Idea comes with built in JDK. We can disable showing this JDK as a option to select for future projects and leave only those we installed in <span style="font-family: "Courier New", Courier, monospace;">C:\Development\Java\OracleJDK\Java8_64bit</span><span style="font-family: inherit;">. In order to do it open project properties and find Platform SDK. Then choose JDK provided by Idea and delete it:</span></div>
<div>
<span style="font-family: inherit;"><br /></span></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEixOVn6GiTSEWnW7V_cFT_yTkRtGnVIGzwfOJQh85OVSHrjVlU6C-Vw8SuJBvUYS6NpUmIv_8zl9X-_nLP-CqLIY36R10-RD9d8MLRdJSfBWBjlyNAdwxVPbjGSjPQXmIc-68dAZjl0iq1k/s1600/2019.01.06_13.png" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="851" data-original-width="1600" height="212" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEixOVn6GiTSEWnW7V_cFT_yTkRtGnVIGzwfOJQh85OVSHrjVlU6C-Vw8SuJBvUYS6NpUmIv_8zl9X-_nLP-CqLIY36R10-RD9d8MLRdJSfBWBjlyNAdwxVPbjGSjPQXmIc-68dAZjl0iq1k/s400/2019.01.06_13.png" width="400" /></a></div>
<div>
<span style="font-family: inherit;"><br /></span></div>
<div>
<span style="font-family: inherit;">Then select our installed Java and change its name:</span></div>
<div>
<span style="font-family: inherit;"><br /></span></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh2e5fLcLzoMXc6U254HKb-CCbU0TnqkkhHQcwSEzmDEr0p6buuMbMarmhNEYwQQ9HBZd4rLW4BJlHOdwuzc_v2mbfFdPTMROTYpMq5EbHsNB0f2EKxQLmmXm-tvwbE3ZNb5QDuU8HlfJxu/s1600/2019.01.06_14.png" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="851" data-original-width="1600" height="212" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh2e5fLcLzoMXc6U254HKb-CCbU0TnqkkhHQcwSEzmDEr0p6buuMbMarmhNEYwQQ9HBZd4rLW4BJlHOdwuzc_v2mbfFdPTMROTYpMq5EbHsNB0f2EKxQLmmXm-tvwbE3ZNb5QDuU8HlfJxu/s400/2019.01.06_14.png" width="400" /></a></div>
<div>
<span style="font-family: inherit;"><br /></span></div>
<div>
<span style="font-family: inherit;">From now this is only one and thus default Java to use in Java projects. Of course You can add other JDK version like OpenJDK.</span></div>
<div>
<span style="font-family: inherit;"><br /></span></div>
<div>
<span style="font-family: inherit;">That's all. Idea is configured and ready for Java development.</span></div><div><span style="font-family: inherit;"><br /></span></div><div><span style="font-family: inherit;"><h2>"Hello World" - sample java application with Git</h2><div><br /></div><div>Using git is beyond of the scope of this blog :) There are many great tutorials explaining the nature of git - for example <a href="https://pl.atlassian.com/git/tutorials/what-is-version-control">this one</a>. I will show how to add a git support to the existing project (Hello World created earlier).</div><div><br /></div><div><b>Step 1: </b>Convert existing projest into git respository. </div><div><br /></div><div>Open project location (<span style="font-family: Courier New, Courier, monospace;">C:\Development\Projects\Test</span>) and using context menu (right mouse click) select option <span style="font-family: Courier New, Courier, monospace;">Git Bash Here</span>. Git will open its terminal with path set to the project. Now enter command <span style="font-family: Courier New, Courier, monospace;">git init:</span></div><div><span style="font-family: Courier New, Courier, monospace;"><br /></span></div><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjOMxefeqotHEwd4lvsfSi8aiW9gO4I53rrmZIC5_ouC_LMb5NgVeX8tyO07w1wjIfvyqonJtzw94Envz7lufUv9diuy1Mkzp-B455ViL4bcsrRSlB1f-YnELQGe5yvnQUc6YNqGX9ENsxR/s1600/2019.01.06_24.png" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="836" data-original-width="1600" height="208" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjOMxefeqotHEwd4lvsfSi8aiW9gO4I53rrmZIC5_ouC_LMb5NgVeX8tyO07w1wjIfvyqonJtzw94Envz7lufUv9diuy1Mkzp-B455ViL4bcsrRSlB1f-YnELQGe5yvnQUc6YNqGX9ENsxR/s400/2019.01.06_24.png" width="400" /></a></div><div><span style="font-family: Courier New, Courier, monospace;"><br /></span>This will create empty repository:</div><div><br /></div><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhBMI9yzzB6veCXO595zQQU_6Isf81NcJ_tb-Hs_Wf6INhu-_VppngLdFH3L4209-Gaw5IYlFPFVt3fV6udKz4u2Y9aVxwfjtOfWwawqC-k1pmzby2__rfgk_THUKXYuQICx3WUUsWCgZ_x/s1600/2019.01.06_25.png" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="432" data-original-width="1222" height="141" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhBMI9yzzB6veCXO595zQQU_6Isf81NcJ_tb-Hs_Wf6INhu-_VppngLdFH3L4209-Gaw5IYlFPFVt3fV6udKz4u2Y9aVxwfjtOfWwawqC-k1pmzby2__rfgk_THUKXYuQICx3WUUsWCgZ_x/s400/2019.01.06_25.png" width="400" /></a></div><div><br /></div><div>The project is now maintained by Git.</div><div><br /></div><div><b>Step 2:</b> Verify git integration under IntelliJ Idea. </div><div><br /></div><div>Open Test project in Idea to see that it is recognized as a git project now:</div><div><br /></div><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEij6Zp6w59_0Da-WDot0SQs1PSHxHNuRvPjabqmDpQImUjeUojfi9U1P5I9Y1cm1XF3nlVQtS9H4_-NT7zWDKdIgbPS1_Ke4E8rOAeZDH2vbVLHzDNGgQP1U9m4QTDyD6bVHInlgdge3VBs/s1600/2019.01.06_26.png" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="856" data-original-width="1600" height="213" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEij6Zp6w59_0Da-WDot0SQs1PSHxHNuRvPjabqmDpQImUjeUojfi9U1P5I9Y1cm1XF3nlVQtS9H4_-NT7zWDKdIgbPS1_Ke4E8rOAeZDH2vbVLHzDNGgQP1U9m4QTDyD6bVHInlgdge3VBs/s400/2019.01.06_26.png" width="400" /></a></div><div><br /></div><div><b>Step 3 (optional):</b> Use git command directly from IntelliJ Idea.</div><div><br /></div><div>Idea has great integration with Git, however it is possible to use git commands entered by hand directly in Idea - just like we use them in Git Bash. We can use Idea's terminal and set it to open Git Bash under the hood. Go to the settings and find options for Termimal. Set shell path to point to the Git Bash executable script with some additional options:</div><div><br /></div><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiLQI0TQJwcs3lwmiWdbCUqQHd1RDcz-kFrbfNME-_v163VCiCAkVYIToQ3eKLST4rTgpgY-eLFedqfiExrDN_e6o8SOEWzDIvOpxxkhrchoCm8fmdH7gWiWSIkIDtDrEdkG_SLV_wu_64J/s1600/2019.01.06_27.png" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="853" data-original-width="1600" height="212" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiLQI0TQJwcs3lwmiWdbCUqQHd1RDcz-kFrbfNME-_v163VCiCAkVYIToQ3eKLST4rTgpgY-eLFedqfiExrDN_e6o8SOEWzDIvOpxxkhrchoCm8fmdH7gWiWSIkIDtDrEdkG_SLV_wu_64J/s400/2019.01.06_27.png" width="400" /></a></div><div><br /></div><div>Now we can execute git command in Idea's terminal:</div><div><br /></div><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjxXIfkxtIheSDoXyqelGy9s36WKInSJZwR3IawPTDDvPQdqZREnstQhlFlVLBspvpekimDkFmjahunI1pfPYl1OSy5VdzCJazTq92oAsnjMiruwg4DGKujUtON3GfEQzu912af29hfjbZT/s1600/2019.01.06_28.png" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="844" data-original-width="1600" height="210" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjxXIfkxtIheSDoXyqelGy9s36WKInSJZwR3IawPTDDvPQdqZREnstQhlFlVLBspvpekimDkFmjahunI1pfPYl1OSy5VdzCJazTq92oAsnjMiruwg4DGKujUtON3GfEQzu912af29hfjbZT/s400/2019.01.06_28.png" width="400" /></a></div></span></div>
<div>
<span style="font-family: inherit;"><br /></span></div>
<h2 style="text-align: left;">Installing Maven</h2><div>
<b><br /></b></div>
<div>
Maven will be used for dependency managment, building and running future apps. Idea comes with bundled Maven, so we do not have install our version. But similar to installing Java, we will install our own version of Maven.</div>
<div>
<br /></div>
<div>
<b>Step 1:</b> Create directory <span style="font-family: Courier New, Courier, monospace;">C:\Development\Maven</span>. Then download Maven (binary zip archive) from <a href="https://maven.apache.org/download.cgi">here</a> and unpack it to the created directory.</div>
<div>
<br /></div>
<div>
<b>Step 2:</b> Create directory <span style="font-family: Courier New, Courier, monospace;">C:\Development\Maven\repository</span> for artifacts downloaded by Maven:</div>
<div>
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEivkOBnjWSLp2rNKFiX0mp46GNgaNXiBadMYn6FioFEvVer373Rk0ppQYfHuX4Cl8Z2gC224A6JlqFkJtmqW6r6WB8bZm_kn5pzneP-x536os0SQHZrenMkasjkGqXlaO3VvqpzXBmtIizE/s1600/2019.01.06_15.png" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="468" data-original-width="834" height="223" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEivkOBnjWSLp2rNKFiX0mp46GNgaNXiBadMYn6FioFEvVer373Rk0ppQYfHuX4Cl8Z2gC224A6JlqFkJtmqW6r6WB8bZm_kn5pzneP-x536os0SQHZrenMkasjkGqXlaO3VvqpzXBmtIizE/s400/2019.01.06_15.png" width="400" /></a></div>
<div>
<br /></div>
<div>
<br /></div>
<div>
<b>Step 3:</b> Tell Maven to use created above directory as a repository. </div>
<div>
<br /></div>
<div>
Locate Maven settings file named <span style="font-family: Courier New, Courier, monospace;">settings.xml</span> and add a line inside:</div>
<div>
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh3OrgjvSRpLDW4UZ5fRE7SXcNUan6T6tfqw3Rj8hS2wNY8d_Q2VQ6ZYA_iNJdcckOkXiPCm0wcDFqOwNEeUFeMyTUjAbSvYq1zYLOhuOplLfd2Y1654SS2rXadjHHL93Rc_ZD-3Fa-nR1E/s1600/2019.01.06_16.png" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="855" data-original-width="1600" height="212" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh3OrgjvSRpLDW4UZ5fRE7SXcNUan6T6tfqw3Rj8hS2wNY8d_Q2VQ6ZYA_iNJdcckOkXiPCm0wcDFqOwNEeUFeMyTUjAbSvYq1zYLOhuOplLfd2Y1654SS2rXadjHHL93Rc_ZD-3Fa-nR1E/s400/2019.01.06_16.png" width="400" /></a></div>
<div>
<br /></div>
<div>
<b>Step 4: </b>Add Maven's bin folder to the system PATH variable in order to use maven commands from console.</div>
<div>
<br /></div>
<div>
<b>Step 5:</b> Verify if Maven is installed correctly. </div>
<div>
<br /></div>
<div>
Open command line and execute command: <span style="font-family: Courier New, Courier, monospace;">mvn -version</span>. You should see similar information:</div>
<div>
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhoJg4b9uKN5LW41KuH8XnykFGseRgfVsrBoeXQ48LtXQFky8e9hs2jDYI_CrSdkbYYhHmC3Kj-nf_-nrDjKjqWYdeVQFC8KsN91MgC_HusEon41Fik5V9cMIsgLghVCjLFzO-boXaFT67l/s1600/2019.01.06_17.png" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="314" data-original-width="1282" height="97" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhoJg4b9uKN5LW41KuH8XnykFGseRgfVsrBoeXQ48LtXQFky8e9hs2jDYI_CrSdkbYYhHmC3Kj-nf_-nrDjKjqWYdeVQFC8KsN91MgC_HusEon41Fik5V9cMIsgLghVCjLFzO-boXaFT67l/s400/2019.01.06_17.png" width="400" /></a></div>
<div>
<br /></div>
<div>
<span style="color: red;">Note 1:</span> Maven displays information about Java used. That's why You have to install Java before and set JAVA_HOME variable.</div>
<div>
<br /></div>
<div>
<span style="color: red;">Note 2:</span> There is no need to set up MAVEN_HOME or M2_HOME system variable like it was done for previous versins of Maven.</div>
<div>
<br /></div><div>Now we can tell IntelliJ Idea to use installed Maven instead of bundled. Open settings, type Maven in search field. Idea will show settings for Maven. Choose Maven installation directory. We may notice that Idea will display recognized version of selected Maven installation and local respoitory path will change to the value set in step 3:</div>
<div>
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEivxqh3TsxE4bCzrjdQkNE9Ccw_TJJrvnyCxl6X07KxQY56FqA35t247gmKaw3XvQZhyphenhyphen8eWZANBwi9L900H_eEtjE4zwwmoPFYXbQMWYWlQ6Dv2qZerxcZKcBSF3drNVuGltZDNgtMnXlhR/s1600/2019.01.06_18.png" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="860" data-original-width="1600" height="213" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEivxqh3TsxE4bCzrjdQkNE9Ccw_TJJrvnyCxl6X07KxQY56FqA35t247gmKaw3XvQZhyphenhyphen8eWZANBwi9L900H_eEtjE4zwwmoPFYXbQMWYWlQ6Dv2qZerxcZKcBSF3drNVuGltZDNgtMnXlhR/s400/2019.01.06_18.png" width="400" /></a></div>
<div>
<br /></div>
<div>
Please note that this setting is not global - it is only for currently opened project. If we want to use our manually installed Maven in other projects, we have to repeat above step in those projects.</div></div></div></div>
<div>
<br /></div>
Paweł Nieściorukhttp://www.blogger.com/profile/12640446237233312925noreply@blogger.comtag:blogger.com,1999:blog-160318223199333541.post-25816700779655161412011-12-08T17:53:00.001+01:002018-12-16T15:22:32.941+01:00JSF 1.2 (myFaces) + RichFaces 3.3.x + IE 9 = troubles<div style="font-family: inherit;">
<span style="font-family: inherit;"><b>Note: </b>below description uses <b>Eclipse Indigo</b>, <b>Tomcat 7.0.28</b>,<b> MyFaces 1.2.12, RichFaces 3.3.3 (Final)</b><span style="font-size: small;"><b>.</b></span></span><br />
<span style="font-family: inherit;"><span style="font-family: inherit;"><br /></span>
<span style="font-family: inherit; font-size: small;"><b>Requirements:</b></span></span></div>
<ul style="font-family: inherit;">
<li><span style="font-family: inherit; font-size: small;">working JSF 1.2 sample application from <a href="http://technology-for-human.blogspot.com/2010/09/jsf-12-hello-world-in-eclipse-with-jsp.html">here</a></span></li>
<li><span style="font-family: inherit; font-size: small;">RichFaces 3.3.x added to the project </span></li>
</ul>
<div style="font-family: inherit;">
<span style="font-family: inherit; font-size: small;"><b>You will learn:</b></span></div>
<ul style="font-family: inherit;">
<li><span style="font-family: inherit;"><span style="font-size: small;">how to fix problems with RichFaces 3.3.x </span><span style="font-size: small;">for IE 9 </span></span></li>
</ul>
<div style="font-family: inherit;">
<span style="font-family: inherit; font-size: small;">Let's assume that You develop JSF 1.2 web application. It does not matter if Your application uses .jsp pages or Facelets with <span style="font-family: "courier new" , "courier" , monospace;">.xhtml</span> pages. I assume that <span style="font-family: "courier new" , "courier" , monospace;">.jsp</span> pages are in use. Then You decided to add some Ajax capabilities, new components and so on. So You simply take RichFaces and add them to the project. This is simple thing:</span><br />
<span style="font-family: inherit; font-size: small;"><b>a)</b> download latest RichFaces 3.3.3 (final), take those jars:<span style="font-family: "courier new" , "courier" , monospace;"> richfaces-api-3.3.3.Final.jar, richfaces-impl-3.3.3.Final.jar, richfaces-ui-3.3.3.Final.jar</span> and place them in <span style="font-family: "courier new" , "courier" , monospace;">WEB-INF\lib</span> folder of Your web application. </span><br />
<span style="font-family: inherit; font-size: small;"><b>b)</b> modify your <span style="font-family: "courier new" , "courier" , monospace;">web.xml</span> file by adding there:</span></div>
<pre class="prettyprint lang-xml"><span style="font-family: inherit;"> <filter>
<display-name>RichFaces Filter</display-name>
<filter-name>richfaces</filter-name>
<filter-class>org.ajax4jsf.Filter</filter-class>
</filter>
<filter-mapping>
<filter-name>richfaces</filter-name>
<servlet-name>Faces Servlet</servlet-name>
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
<dispatcher>INCLUDE</dispatcher>
</filter-mapping></span></pre>
<span style="font-family: inherit;">c) add proper taglib directives to each Your jsp page (note that for .xhtml pages syntax is different):</span><br />
<pre class="prettyprint lang-xml"><span style="font-family: inherit;"><%@ taglib uri="http://richfaces.org/a4j" prefix="a4j"%>
<%@ taglib uri="http://richfaces.org/rich" prefix="rich"%></span></pre>
<span style="font-family: inherit;">Now You can use RichFaces components. Let's try to modify <a href="http://technology-for-human.blogspot.com/2010/09/jsf-12-hello-world-in-eclipse-with-jsp.html">our sample JSF 1.2 application</a> by creating new web project named "JSFRichFaces". Then let's add ajax button (<span style="font-family: "courier new" , "courier" , monospace;"><a4j:commanButton></span>) and expandable toggle panel (<span style="font-family: "courier new" , "courier" , monospace;"><rich:simpleTogglePanel></span>). All modifications are shown below:</span><br />
<span style="font-family: inherit;"><span style="font-family: inherit;"><br /></span>
</span><br />
<div class="separator" style="clear: both; text-align: center;">
</div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiNYFmduM80lX470JF3s12e9mFu8ZbeGOQUqDlbFKMwND5ED0ay7yi9H4K3-k9CSKjrVXgXlQQzj5DY4CNi7mgZ76sqMPXA44RyVjKsjEW_kpLWkwZHMJmyPA04pk9sl7H2yywsAK7DMv3o/s1600/2011.12.08_1.PNG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><span style="font-family: inherit;"><img border="0" height="226" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiNYFmduM80lX470JF3s12e9mFu8ZbeGOQUqDlbFKMwND5ED0ay7yi9H4K3-k9CSKjrVXgXlQQzj5DY4CNi7mgZ76sqMPXA44RyVjKsjEW_kpLWkwZHMJmyPA04pk9sl7H2yywsAK7DMv3o/s400/2011.12.08_1.PNG" width="400" /></span></a></div>
<span style="font-family: inherit;"><span style="font-family: inherit;"><br /></span>
<span style="font-family: inherit;"><br /></span>
<span style="font-family: inherit;">Everything works OK under Firefox, Chrome, IE 7, IE 8 - unless You use <b>Internet Explorer 9</b>. You can observe<b> two problems</b>: with RichFaces ajax components and RichFaces components layout.</span></span><br />
<span style="font-family: inherit;">RichFaces 3.3.x does not fully support IE 9. RichFaces developers encourage everyone to migrate into RichFaces 4.x which are IE 9 compatible. The main problem is that <b>RichFaces 4.x works only with JSF 2.x application</b>, and this is not so easy to migrate If You have many clients where You already delivered Your application...</span><br />
<span style="font-family: inherit;"><span style="font-family: inherit;"><br /></span>
<span style="font-family: inherit;"><b>Problem 1:</b> ajax components do not work (some JavaScript errors).</span></span><br />
<span style="font-family: inherit;"><span style="font-family: inherit;"><br /></span>
<span style="font-family: inherit;">When You try to press ajax button named "Go (RichFaces) " <b>under IE 9</b>, You can observe following error (open IE Developer's Toolbar Java Script console before):</span></span><br />
<span style="font-family: inherit;"><span style="font-family: inherit;"><br /></span>
</span><br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgUj2GuI3pRufoBy6-g-gaR0eOLiqHghq1AZOfAndQArr7odYG2z5U8XvUsB9pnh4elgju3eUdjuBMCoPYMauVZPo_wFzqKk7kavf030FVYCH1oLlmtx9dsP0VQ7IrK3fCZed-jVmD6bbxn/s1600/2011.12.08_2.PNG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><span style="font-family: inherit;"><img border="0" height="242" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgUj2GuI3pRufoBy6-g-gaR0eOLiqHghq1AZOfAndQArr7odYG2z5U8XvUsB9pnh4elgju3eUdjuBMCoPYMauVZPo_wFzqKk7kavf030FVYCH1oLlmtx9dsP0VQ7IrK3fCZed-jVmD6bbxn/s400/2011.12.08_2.PNG" width="400" /></span></a></div>
<span style="font-family: inherit;"><span style="font-family: inherit;"><br /></span>
</span><br />
<div class="separator" style="clear: both; text-align: center;">
</div>
<span style="font-family: inherit;"><b><span style="color: red;">Solution:</span> </b>You have to force IE 9 to act as IE 8, where RichFaces 3.3.x works OK. In order to do it You have to define X-UA-Compatible header for each page. You can do it in two ways:</span><br />
<span style="font-family: inherit;"><b>a)</b> add a special <span style="font-family: "courier new" , "courier" , monospace;"><meta></span> element for each page or</span><br />
<span style="font-family: inherit;"><b>b)</b> write a special filter which adds this header to the HTTP response</span><br />
<span style="font-family: inherit;"><span style="font-family: inherit;"><br /></span>
<span style="font-family: inherit;">In our simple case it is sufficient to add a <span style="font-family: "courier new" , "courier" , monospace;"><meta></span> into the page <span style="font-family: "courier new" , "courier" , monospace;"><head></span> section:</span></span><br />
<pre class="prettyprint lang-xml"><span style="font-family: inherit;"><meta http-equiv="X-UA-Compatible" content="IE=EmulateIE8" /></span></pre>
<span style="font-family: inherit;">Remember that this <meta> <b>must be placed as first element</b> in <span style="font-family: "courier new" , "courier" , monospace;"><head></span> section!</span><br />
<span style="font-family: inherit;"><span style="font-family: inherit;"><br /></span>
<span style="font-family: inherit;"><b>Problem 2: </b>RichFaces components have broken layout (problems with CSS).</span></span><br />
<span style="font-family: inherit;"><span style="font-family: inherit;"><br /></span>
<span style="font-family: inherit;">When You open our sample application <b>under IE 9</b>, toggle panel has no CSS styles set at all. Under Chrome or Firefox everything looks OK. Checking HTTP request/response headers shows that for IE 9 proper CSS for RichFaces component (CSS located in the .jar file) is not loaded and server returns error:</span></span><br />
<span style="font-family: inherit;"><span style="font-family: inherit;"><br /></span>
</span><br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhWcsucyYlQ2P59kiMowSUlk9i66OqWm8x230maXgJ3fP0HPqIRPux5CWfTadOGrajYDf_YJHgwARuQm-0EQro00kqdqT1WqTnuWKIWNp8s30x9yOo1t6_sA1V9gzVHd-qu7ykoPcwCqmDr/s1600/2011.12.08_3.PNG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><span style="font-family: inherit;"><img border="0" height="251" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhWcsucyYlQ2P59kiMowSUlk9i66OqWm8x230maXgJ3fP0HPqIRPux5CWfTadOGrajYDf_YJHgwARuQm-0EQro00kqdqT1WqTnuWKIWNp8s30x9yOo1t6_sA1V9gzVHd-qu7ykoPcwCqmDr/s400/2011.12.08_3.PNG" width="400" /></span></a></div>
<span style="font-family: inherit;"><span style="font-family: inherit;"><br /></span>
<span style="font-family: inherit;"><br /></span>
<span style="font-family: inherit;">Console under Eclipse shows that exception is thrown:</span></span><br />
<div style="text-align: left;">
<span style="font-family: inherit;"><br /></span></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhhY2xfrMTjj5DnA0aD8Yk3SPvttI060jlatUIy-DwF_Cg6VEXtI0VSD7pqXTZtBTPTofK6B7ErtR8_XEmP4WZB8d3vSmSIe5XFQ9PyEswfn1tQO4u4at2wQfqEh5D0MNtJhaUsNRRMpN4H/s1600/2011.12.08_4.PNG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><span style="font-family: inherit;"><img border="0" height="242" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhhY2xfrMTjj5DnA0aD8Yk3SPvttI060jlatUIy-DwF_Cg6VEXtI0VSD7pqXTZtBTPTofK6B7ErtR8_XEmP4WZB8d3vSmSIe5XFQ9PyEswfn1tQO4u4at2wQfqEh5D0MNtJhaUsNRRMpN4H/s400/2011.12.08_4.PNG" width="400" /></span></a></div>
<div class="separator" style="clear: both; text-align: left;">
</div>
<div class="separator" style="clear: both; text-align: left;">
</div>
<span style="font-family: inherit;"><span style="font-family: inherit;"><br /></span>
<span style="font-family: inherit;">Under Firefox everything looks OK:</span></span><br />
<span style="font-family: inherit;"><span style="font-family: inherit;"><br /></span>
</span><br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhWZwJW7Ir3Ggu4R8l2vcPZj94X0BdxZFiaJLgUMPhkItWyjBC8Z4rLwQ1jwoaudV0ZVx0lLOh-oyP7ckvMn_7U9o5DFLnMlHoZ_opV3dU7RhgfpiRehZrdss90oTN_KjGstgq5syUp9PDK/s1600/2011.12.08_5.PNG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><span style="font-family: inherit;"><img border="0" height="237" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhWZwJW7Ir3Ggu4R8l2vcPZj94X0BdxZFiaJLgUMPhkItWyjBC8Z4rLwQ1jwoaudV0ZVx0lLOh-oyP7ckvMn_7U9o5DFLnMlHoZ_opV3dU7RhgfpiRehZrdss90oTN_KjGstgq5syUp9PDK/s400/2011.12.08_5.PNG" width="400" /></span></a></div>
<span style="font-family: inherit;"><span style="font-family: inherit;"><br /></span>
</span><br />
<div class="separator" style="clear: both; text-align: center;">
</div>
<span style="font-family: inherit;"><span style="font-family: inherit;"><br /></span>
<span style="font-family: inherit;"><b>Why it happens?</b> Accroding to that <a href="http://blogs.msdn.com/b/ie/archive/2010/10/26/mime-handling-changes-in-internet-explorer.aspx">article</a>, there was a <b>change in IE 9 for MIME type handling</b>. IE 9 ignores CSS styles if they are not delivered with a <b>text/css</b> MIME type.</span></span><br />
<span style="font-family: inherit;">Why it works for previous IE 7 and IE 8? Because also HTTP Request Accept header sent by IE 9 is different than sent from IE 7 or 8. IE 7/8 sent <b style="font-family: "Courier New",Courier,monospace;">text/css, */*</b> as Accept, where IE 9 sends <b>only <span style="font-family: "courier new" , "courier" , monospace;">text/css</span></b> as Accept.</span><br />
<span style="font-family: inherit;">Knowing that it is time to look inside myFaces source, to find the code which produces mentioned exception. Everything is located in HtmlRendererUtils.java (located in myfaces-impl-1.2.11.jar) in the selectContentType() method. It works for IE 7/8 because */* sent in Accept by IE 7/8 is on supported content type list, where text/css is not recognized at all.</span><br />
<span style="font-family: inherit;"><span style="font-family: inherit;"><br /></span>
<span style="font-family: inherit;"><b style="color: red;">Solution:</b> add <span style="font-family: "courier new" , "courier" , monospace;">text/css</span> support, then recompile <span style="font-family: "courier new" , "courier" , monospace;">HtmlRendererUtils.java</span> and replace it in <span style="font-family: "courier new" , "courier" , monospace;">myfaces-impl-1.2.11.jar</span> (or build complete myfaces-impl-1.2.11.jar). In order to avoid this mumbo-jumbo with Maven to build whole <span style="font-family: "courier new" , "courier" , monospace;">.jar</span>, just create simple Java Project, create a <span style="font-family: "courier new" , "courier" , monospace;">HtmlRendererUtils.java</span> in certain package, change it and compile, then replace compiled class in <span style="font-family: "courier new" , "courier" , monospace;">myfaces-impl-1.2.11.jar</span> ;-)</span></span><br />
<span style="font-family: inherit;"><span style="font-family: inherit;"><br /></span>
</span><br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj-EAtrxokrcDiFYwjWgO-_0Scj9aCf-FmqkN84yx8MQae_ozEznUzVGcVyuf4Q9xYWBf8V1sS4Q1dOWA_YJH9t9KpUqeV5TZHSlrFV6L-OLw8jIlUQPS1eZ9A1gnFB6EmecjEppLACv_HP/s1600/2011.12.08_6.PNG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><span style="font-family: inherit;"><img border="0" height="242" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj-EAtrxokrcDiFYwjWgO-_0Scj9aCf-FmqkN84yx8MQae_ozEznUzVGcVyuf4Q9xYWBf8V1sS4Q1dOWA_YJH9t9KpUqeV5TZHSlrFV6L-OLw8jIlUQPS1eZ9A1gnFB6EmecjEppLACv_HP/s400/2011.12.08_6.PNG" width="400" /></span></a></div>
<span style="font-family: inherit;"><span style="font-family: inherit;"><br /></span>
<span style="font-family: inherit;"><br /></span>
<span style="font-family: inherit;">Or download recompiled <span style="font-family: "courier new" , "courier" , monospace;">myfaces-impl-1.2.11.jar</span> from <a href="https://drive.google.com/open?id=1dBVknF9r_Cti3XW0_dEbqQblUeuMe_L7">here</a>.</span></span><br />
<span style="font-family: inherit;"><span style="font-family: inherit;"><br /></span>
<span style="font-family: inherit;">That's all. IE 9 works again without need to add Your web page to its compatibility view list or something. And You have time to migrate to JSF 2.x and RichFaces 4.x before IE 10 comes to market...</span></span>Paweł Nieściorukhttp://www.blogger.com/profile/12640446237233312925noreply@blogger.com9tag:blogger.com,1999:blog-160318223199333541.post-50480241304713352542011-09-21T21:47:00.001+02:002018-12-16T14:17:38.947+01:00Tomcat - using Realm to protect access to JSF application (part 2 of 2)<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><b>Note: </b>below description uses <b>Eclipse Indigo</b>, <b>Tomcat 7.0.28</b>,<b> MyFaces 2.1.7</b></span><br />
<span style="font-family: inherit;"><i><b> </b></i><b> </b></span><br />
<b><span style="font-family: inherit;">Requirements:</span></b></div>
<ul style="font-family: Verdana,sans-serif;">
<li><span style="font-family: inherit;">understanding basics of Tomcat's Realm</span></li>
</ul>
<div style="font-family: Verdana,sans-serif;">
<b><span style="font-family: inherit;">You will learn:</span></b></div>
<ul style="font-family: Verdana,sans-serif;">
<li><span style="font-family: inherit;">how to define users and roles in own web applications (JSF 2.0 as example)</span></li>
<li><span style="font-family: inherit;">how to use encrypted passwords for realm users instead od plain text</span></li>
</ul>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;">In the <a href="http://technology-for-human.blogspot.com/2011/09/tomcat-change-default-application.html">previous post</a> I described how to configure access to Tomcat's server administration application using Tomcat's Realm elements. The whole thing boiled down to write a few lines of text in <span style="font-family: "Courier New",Courier,monospace;">server.xml</span> file. But how does it look like when we have to do it from scratch in our application?</span></div>
<div style="font-family: Verdana,sans-serif;">
</div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><br /></span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><b>Step 1.</b> Let's create very simple JSF application which has only 3 pages:</span></div>
<ul style="font-family: Verdana,sans-serif;">
<li><span style="font-family: inherit;"><span style="font-family: "Courier New",Courier,monospace;">/index.html</span> - main page with two buttons navigating into two pages:</span></li>
<li><span style="font-family: inherit;"><span style="font-family: "Courier New",Courier,monospace;">/unrestrictedPage.xhtml</span> - page always accessible and visible to anyone</span></li>
<li><span style="font-family: inherit;"><span style="font-family: "Courier New",Courier,monospace;">/restricted/restrictedPage.xhtml</span> - page and directory available only for valid users</span></li>
</ul>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;">The complete application structure will look like:</span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><br /></span></div>
<div class="separator" style="clear: both; text-align: center;">
</div>
<div style="font-family: Verdana,sans-serif;">
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjFX1UP8W0ApXF54EZpWGh_c4Z7q104_MgQfeXzdlpqu8pQZga3agTPnfpkG-c8YQ-2TfkuV_H2P9tS6i7j7AGi5xrWp42L_UkzQ7jMD-KT5jEEOvPo8OkQPT6NRA-79p7fKgeUNdgFO0qk/s1600/2011.09.21_1.PNG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><span style="font-family: inherit;"><img border="0" height="301" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjFX1UP8W0ApXF54EZpWGh_c4Z7q104_MgQfeXzdlpqu8pQZga3agTPnfpkG-c8YQ-2TfkuV_H2P9tS6i7j7AGi5xrWp42L_UkzQ7jMD-KT5jEEOvPo8OkQPT6NRA-79p7fKgeUNdgFO0qk/s320/2011.09.21_1.PNG" width="320" /></span></a></div>
<span style="font-family: inherit;"><br /></span>
<span style="font-family: inherit;"><br /></span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><b>Step 2:</b> we have to add a privilleged user named <span style="font-family: "Courier New",Courier,monospace;">admin</span> with password <span style="font-family: "Courier New",Courier,monospace;">adminpass</span> who belongs to role <span style="font-family: "Courier New",Courier,monospace;">privillegedUsers</span> (You can choose any user login, password and role name). Just simply add this line:</span></div>
<pre class="prettyprint lang-xml" style="font-family: Verdana,sans-serif;"><span style="font-family: inherit;"><user username="admin" password="adminpass" roles="privillegedUsers" /></span></pre>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;">into <span style="font-family: "Courier New",Courier,monospace;">[tomcat directory]\conf\tomcat-users.xml</span> file.</span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><br /></span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><b>Step 3:</b> configuring application. We have to modify <span style="font-family: "Courier New",Courier,monospace;">web.xml</span> of our sample application by adding following text:</span></div>
<pre class="prettyprint lang-xml" style="font-family: Verdana,sans-serif;"><span style="font-family: inherit;"><web-app>
...
<security-role>
<description>
All persons belong to that role have access to restricted application area.
</description>
<role-name>privillegedUsers</role-name>
</security-role>
<security-constraint>
<web-resource-collection>
<web-resource-name>Restricted</web-resource-name>
<url-pattern>/faces/restricted/*</url-pattern>
<url-pattern>/restricted/*</url-pattern>
</web-resource-collection>
<auth-constraint>
<role-name>privillegedUsers</role-name>
</auth-constraint>
</security-constraint>
<login-config>
<auth-method>BASIC</auth-method>
<realm-name>Restricted access</realm-name>
</login-config>
...
</web-app></span></pre>
<div style="font-family: Verdana,sans-serif;">
</div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;">First we defined a role named <span style="font-family: "Courier New",Courier,monospace;">privillegedUsers</span> - the same we put in <span style="font-family: "Courier New",Courier,monospace;">tomcat-users.xml</span> file above. Second we have to defined prottected parts of our application, by providing proper URL pattern. Please note that we have two entries here: <span style="font-family: "Courier New",Courier,monospace;">/restricted/*</span> - everything in this directory is protected in case someone typed this part manually in browser's address bar, <span style="font-family: "Courier New",Courier,monospace;">/faces/restricted/*</span> - JSF adds "faces" prefix when navigating between pages, so the URL is a little bit different and we have to watch for this also. After that we have to define a role which is allowed to access to the protected parts. This is <span style="font-family: "Courier New",Courier,monospace;">privillegedUsers</span> role defined earlier. At the end we have to define the way how the authentication is done - the simplest way is BASIC, which displays predefined form with credentials.</span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><br /></span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;">Now we are ready to deploy our application on Tomcat and run it. It should look like this:</span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><br /></span>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjIuOTQJDDC9SX8DiGA6phG8ups_wcj-tgFtY0cHKHf6vE3fGQQVukNJ_4l4HJ6xT_WcGF7j4C1Z_3WUYjA7VzHb_th7e5j1qM0pPWzPCMnuKCpDiFFUKWGui5MAJeE_rCaoqDFbF2eLSf3/s1600/2011.09.21_2.PNG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><span style="font-family: inherit;"><img border="0" height="198" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjIuOTQJDDC9SX8DiGA6phG8ups_wcj-tgFtY0cHKHf6vE3fGQQVukNJ_4l4HJ6xT_WcGF7j4C1Z_3WUYjA7VzHb_th7e5j1qM0pPWzPCMnuKCpDiFFUKWGui5MAJeE_rCaoqDFbF2eLSf3/s400/2011.09.21_2.PNG" width="400" /></span></a></div>
<span style="font-family: inherit;"><br /></span></div>
<div style="font-family: Verdana,sans-serif;">
</div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;">when we try to access to restricted area. In order to do this we have to put credentials for user defined in step 2 (<span style="font-family: "Courier New",Courier,monospace;">admin</span>, <span style="font-family: "Courier New",Courier,monospace;">adminpass</span>).</span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><br /></span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><b style="color: red;">Note:</b> there is a little trick here, for the button "Restricted area" - see <span style="font-family: "Courier New",Courier,monospace;">index.xhtml</span> page source code:</span></div>
<pre class="prettyprint lang-xml" style="font-family: Verdana,sans-serif;"><span style="font-family: inherit;"><h:commandButton value="Resricted area" action="/restricted/restrictedPage.xhtml?faces-redirect=true" />
<h:commandButton value="Unrestricted area" action="/unrestrictedPage.xhtml" /></span></pre>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;">Because JSF internally by default makes forward to other page, browser is unaware what has happened and display URL from one step back. In such case our URL security patterns will not match anything, and <span style="font-family: "Courier New",Courier,monospace;">restrictedPage.xhtml</span> will be displayed without asking for login and password! In order to make protection work we have to perform full redirection for that action in order to force browser to fetch target URL and display it. For JSF applications it is better to use filters or Spring Security in order to avoid such dirty tricks.</span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><br /></span></div>
<div style="font-family: Verdana,sans-serif;">
<b><span style="font-family: inherit;">Encrypted passwords.</span></b></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><br /></span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;">As You probably have seen, passwords vor valid users are stored inside tomcat-users.xml file as a plain text. There is a possibility to store them in encrypted form, using MD5. Here is what needs to be done:</span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><br /></span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><b>Step 1:</b> using Eclipse modify Tomcat <span style="font-family: "Courier New",Courier,monospace;">server.xml</span> and its <span style="font-family: "Courier New",Courier,monospace;"><Realm></span> atrribute into:</span></div>
<pre class="prettyprint lang-xml" style="font-family: Verdana,sans-serif;"><span style="font-family: inherit;"><Realm className="org.apache.catalina.realm.UserDatabaseRealm" resourceName="UserDatabase" digest="MD5"/></span></pre>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><b>Step 2:</b> in <span style="font-family: "Courier New",Courier,monospace;">web.xml</span> change <span style="font-family: "Courier New",Courier,monospace;"><auth-method></span> from BASIC to DIGEST</span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><br /></span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><b>Step 3:</b> for user <span style="font-family: "Courier New",Courier,monospace;">admin</span> and his previous password <span style="font-family: "Courier New",Courier,monospace;">adminpass</span> we have to generate its md5 equivalent using Tomcat's <span style="font-family: "Courier New",Courier,monospace;">digest.bat</span> file (located in <span style="font-family: "Courier New",Courier,monospace;">[tomcat directory]\bin</span> folder) :</span></div>
<div style="font-family: "Courier New",Courier,monospace;">
<span style="font-family: inherit;"><br /></span></div>
<div style="font-family: "Courier New",Courier,monospace;">
<span style="font-family: inherit;">digest.bat -a md5 admin:"Restricted access":adminpass</span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><br /></span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;">"Restricted access" is a realm name taken from <span style="font-family: "Courier New",Courier,monospace;"><auth-method></span> tag from <span style="font-family: "Courier New",Courier,monospace;">web.xml</span>. Those names <b>must match</b>, if name contains spaces, it must be surrounded with "".</span><br />
<b><span style="font-family: inherit;"><br />
</span></b><br />
<span style="font-family: inherit;"><b>Step 4:</b> using Eclipse modify Tomcat's <span style="font-family: "Courier New",Courier,monospace;">tomcat-users.xml</span> file:</span></div>
<pre class="prettyprint lang-xml" style="font-family: Verdana,sans-serif;"><span style="font-family: inherit;"><user username="admin" password="db7bc05adcf611fc779f32a4e680cc01" roles="privillegedUsers" /> </span></pre>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;">where password was taken as a result of executing command from step 3.</span><br />
<span style="font-family: inherit;"><br /></span>
<div style="color: red;">
<span style="font-family: inherit;">------------------------------------------- </span></div>
<div style="color: red;">
<span style="font-family: inherit;">Download source files:</span></div>
<div style="font-family: Verdana,sans-serif;">
<br /></div>
<b>Note:</b> make sure that <a href="http://technology-for-human.blogspot.com/2009/04/instalacja-java-jdk-prawie-bezbolesna.html">Java</a>, <a href="http://technology-for-human.blogspot.com/2009/04/instalacja-i-konfiguracja-ide-eclipse.html">Eclipse</a> and <a href="http://technology-for-human.blogspot.com/2009/05/konfiguracja-tomcata-w-eclipse.html">Tomcat</a> are properly installed and configured for running the project (additional configuration may be required if different directories are used).<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><br /></span></div>
<span style="font-family: inherit;">Eclipse complete sample project is <a href="https://sites.google.com/sithttps://sites.google.com/site/technologyforhuman/JSF2TomcatBasicSecurity.zip">here</a> (with all required libraries).</span></div>
Paweł Nieściorukhttp://www.blogger.com/profile/12640446237233312925noreply@blogger.com1tag:blogger.com,1999:blog-160318223199333541.post-43838165183025790202011-09-13T22:01:00.000+02:002017-02-10T18:07:32.463+01:00Tomcat - change default application, protecting access to web applications using Realm (part 1 of 2)<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><b>Note: </b>below description uses <b>Eclipse Indigo</b>, <b>Tomcat 7.0.28</b>,<b> </b></span><br />
<span style="font-family: inherit;"><br /></span>
<b><span style="font-family: inherit;">Requirements:</span></b></div>
<ul style="font-family: Verdana,sans-serif;">
<li><span style="font-family: inherit;">installed Java (description <a href="http://technology-for-human.blogspot.com/2009/04/instalacja-java-jdk-prawie-bezbolesna.html">here</a>)</span></li>
<li><span style="font-family: inherit;">installed and configured Eclipse (description <a href="http://technology-for-human.blogspot.com/2009/04/instalacja-i-konfiguracja-ide-eclipse.html">here</a>)</span></li>
<li><span style="font-family: inherit;">installed and confiured Tomcat for the Eclipse (description <a href="http://technology-for-human.blogspot.com/2009/05/konfiguracja-tomcata-w-eclipse.html">here</a>)</span></li>
</ul>
<div style="font-family: Verdana,sans-serif;">
<b><span style="font-family: inherit;">You will learn:</span></b></div>
<ul style="font-family: Verdana,sans-serif;">
<li><span style="font-family: inherit;">how to change default Tomcat application</span></li>
<li><span style="font-family: inherit;">how to allow users access to Tomcat's server administration application using Tomcat's Realm</span></li>
</ul>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><br /></span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;">Let's assume that You have Tomcat which is configured to work in Eclipse. When You start Tomcat (using Eclipse or standard scripts from Tomcat's distribution) and type in the browser URL <b>http://localhost:8080</b>, You will see Tomcat's default application:</span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><br /></span></div>
<div class="separator" style="clear: both; text-align: center;">
</div>
<div style="font-family: Verdana,sans-serif;">
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEigxpnnesTMkI-DyWfCwsWM5lkzsPB7llobd3szB5jzIh39QCmWN5i3B-ZKV7OKj64TvDd_xIhhGn9K9ZPDj5VLQe9V2XF0UKDJk6nbSeX-Cx93ORuOD49avp2LIpz-ZgvI23DNwLxEZ_vk/s1600/2011.09.13_1.PNG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><span style="font-family: inherit;"><img border="0" height="208" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEigxpnnesTMkI-DyWfCwsWM5lkzsPB7llobd3szB5jzIh39QCmWN5i3B-ZKV7OKj64TvDd_xIhhGn9K9ZPDj5VLQe9V2XF0UKDJk6nbSeX-Cx93ORuOD49avp2LIpz-ZgvI23DNwLxEZ_vk/s400/2011.09.13_1.PNG" width="400" /></span></a></div>
</div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><br /></span>
<span style="font-family: inherit;">This application is located in the <span style="font-family: "Courier New",Courier,monospace;">[tomcat directory]\webapps\ROOT</span> folder. If You want to <b>change this page</b>, just edit content of file <span style="font-family: "Courier New",Courier,monospace;">index.jsp</span> and <span style="font-family: "Courier New",Courier,monospace;">index.html</span>. If You want to <b>completely remove this application</b>, just remove complete ROOT folder.</span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;">Removing is not the best choice, because Tomcat is shipped with a special applications for server administration. You can access those applications using links in red square. Try to click on any link - You should see screen with login and password prompt:</span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><br /></span>
<div style="color: red;">
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgJXMYweNcOglbhtNfD0VcajYEamLBg8OtuMwsQ7miZJhzLgkRpNS1ZVEk-dGWE37Bk3VPEfzCt3zJzTNNK6pCPwJxm6bapOqSZdLeac8gghLuqCVMD-5eBmfd314AuHcrHkdmNlV9qN6k2/s1600/2011.09.13_2.PNG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><span style="font-family: inherit;"><img border="0" height="242" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgJXMYweNcOglbhtNfD0VcajYEamLBg8OtuMwsQ7miZJhzLgkRpNS1ZVEk-dGWE37Bk3VPEfzCt3zJzTNNK6pCPwJxm6bapOqSZdLeac8gghLuqCVMD-5eBmfd314AuHcrHkdmNlV9qN6k2/s400/2011.09.13_2.PNG" width="400" /></span></a></div>
<span style="font-family: inherit;"><br /></span></div>
</div>
<div class="separator" style="clear: both; text-align: center;">
</div>
<div style="font-family: Verdana,sans-serif;">
</div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;">Those parts are protected using <b>Tomcat Realm</b>. What is Realm? Realm is a set of valid users (defined by user name and password) and roles where those users belong. The idea is to configure access to web application only for valid users from certain roles. But where are those users and roles stored? It depends on a implementation of a realm - they can be stored in database, in LDAP, or in xml file. Tomcat's server administration application uses xml file to define users' access. Let's try to set up some users able to start that application.</span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><br /></span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><b>Step 1:</b> using Eclipse open <span style="font-family: "Courier New",Courier,monospace;">server.xml</span> from the Servers, and make sure that entry <span style="font-family: "Courier New",Courier,monospace;"><Realm classname="org.apache.catalina.realm.UserDatabaseRealm" resourcename="UserDatabase"></Realm></span> exsits between <span style="font-family: "Courier New",Courier,monospace;"><Engine></span> tags, outside <span style="font-family: "Courier New",Courier,monospace;"><Host&gt</span> tags - it means that this realm will be used for all hosts and all applications on that hosts:</span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><br /></span></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhtellck7-LxPILOlMJYyn8JxU98gZQGbzPQctt9u49Y5RQaocokZ85vrwqNaG1ascdRk5bbLuRWsfBdEELHmU3aR6LDG57ArBuPlBrEOvTm40jcmoJPJXavXf-LTkbrbRjgMZOuY8U27lD/s1600/2011.09.13_3.PNG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><span style="font-family: inherit;"><img border="0" height="130" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhtellck7-LxPILOlMJYyn8JxU98gZQGbzPQctt9u49Y5RQaocokZ85vrwqNaG1ascdRk5bbLuRWsfBdEELHmU3aR6LDG57ArBuPlBrEOvTm40jcmoJPJXavXf-LTkbrbRjgMZOuY8U27lD/s400/2011.09.13_3.PNG" width="400" /></span></a></div>
<div class="separator" style="clear: both; color: red; text-align: left;">
<span style="font-family: inherit;"><br /></span></div>
<div style="font-family: Verdana,sans-serif;">
</div>
<div style="font-family: Verdana,sans-serif;">
</div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><b>Step 2:</b> using Eclipse open <span style="font-family: "Courier New",Courier,monospace;">tomcat-users.xml</span> from the Servers and add an entry for the user who will be able to access Tomcat's server administration application:</span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><br /></span>
<div style="color: red;">
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh1yI-bFGfFiFU5mKJ9FeXpKVKT6Srnv8bIG7LMZLVbUrXMClx7sh8n1Fpi8Cvzm5PKw_12dtT4MvxfrjWotKHWdulQOHWuzp8cJRU5ipVjfD8nS7w6r8VL9ZqN6CTGu6Z8rpt_8DuLkg2J/s1600/2011.09.13_4.PNG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><span style="font-family: inherit;"><img border="0" height="115" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh1yI-bFGfFiFU5mKJ9FeXpKVKT6Srnv8bIG7LMZLVbUrXMClx7sh8n1Fpi8Cvzm5PKw_12dtT4MvxfrjWotKHWdulQOHWuzp8cJRU5ipVjfD8nS7w6r8VL9ZqN6CTGu6Z8rpt_8DuLkg2J/s400/2011.09.13_4.PNG" width="400" /></span></a></div>
<span style="font-family: inherit;"><br /></span></div>
</div>
<div class="separator" style="clear: both; text-align: center;">
</div>
<div style="font-family: Verdana,sans-serif;">
</div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;">By default UserDatabaseRealm uses <span style="font-family: "Courier New",Courier,monospace;">[tomcat directory]\conf\tomcat-users.xml </span>file to load users and their roles into memory on server startup. In order to allow defined users to access administration application, they need to belong to roles named <b>manager-status</b>, <b>manager-gui</b> and <b>admin-gui</b>. After changes made, restart Tomcat server and try to access Tomcat's administration application giving username and password from tomcat-users.xml file. If everything was set up OK, You should see mentioned applications.</span><br />
<span style="font-family: inherit;"><br /></span>
<span style="font-family: inherit;">You may wonder why we used here roles named <b>manager-status</b> or <b>manager-gui</b> or <b>admin-gui</b>. Those role names come from Tomcat's server administration application specific settings, stored in the <span style="font-family: "Courier New",Courier,monospace;">web.xml</span> file. In the next post I will show how to protect own application (JSF2 application will be used as an example) and how to define own roles.</span></div>
Paweł Nieściorukhttp://www.blogger.com/profile/12640446237233312925noreply@blogger.com0tag:blogger.com,1999:blog-160318223199333541.post-87515355721507977992011-08-04T22:15:00.001+02:002018-12-16T14:14:27.048+01:00Sending emails using Java Mail and Google Mail account<div style="font-family: Verdana,sans-serif;">
<b><span style="font-family: inherit;">Requirements:</span></b></div>
<ul style="font-family: Verdana,sans-serif;">
<li><span style="font-family: inherit;">installed Java (description here)</span></li>
<li><span style="font-family: inherit;">installed and configured Eclipse (description here)</span></li>
<li><span style="font-family: inherit;">valid Google Mail account </span></li>
</ul>
<div style="font-family: Verdana,sans-serif;">
<b><span style="font-family: inherit;">You will learn:</span></b></div>
<ul style="font-family: Verdana,sans-serif;">
<li><span style="font-family: inherit;">how to send e-mails from Java application using Java Mail and Google Mail account.</span></li>
</ul>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><br /></span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;">Sometimes You need to send an email from Your Java application. In order to do this You have to use valid e-mail account, and You have to know the address of SMTP server for this account. And of course You have to write some code which will connect to the SMTP server (using Your account credentials) and then create and send email. A Google Mail account will be used as an example.</span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><br /></span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><b>Step 1:</b> create clean Java Eclipse Project</span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><br /></span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><b>Step 2:</b> download JavaMail API 1.4.4 (javamail_1_4_4.zip) from <a href="http://www.oracle.com/technetwork/java/javasebusiness/downloads/java-archive-downloads-eeplat-419426.html#javamail-1.4.4-oth-JPR">here</a>.</span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><br /></span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;">After downloading JavaMail, extract it and go into its /lib folder. Copy all .jar files from that directory into Your project and add them to the build path.</span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><br /></span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><b>Step 3:</b> write a simple code for sending email using Google Mail. You can use TLS or SSL connection to Google SMTP server.</span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><br /></span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><b>a) </b>using TLS connection to SMTP server:</span></div>
<pre class="prettyprint lang-java" style="font-family: Verdana,sans-serif;"><span style="font-family: inherit;">// ...
import java.util.Properties;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
// ...
Properties propsTLS = new Properties();
propsTLS.put("mail.transport.protocol", "smtp");
propsTLS.put("mail.smtp.host", "smtp.gmail.com");
propsTLS.put("mail.smtp.auth", "true");
propsTLS.put("mail.smtp.starttls.enable", "true"); // GMail requires STARTTLS
Session sessionTLS = Session.getInstance(propsTLS);
sessionTLS.setDebug(true);
Message messageTLS = new MimeMessage(sessionTLS);
messageTLS.setFrom(new InternetAddress("random_sender_4568744122@gmail.com", "John Smith"));
messageTLS.setRecipients(Message.RecipientType.TO, InternetAddress.parse("random_recipient_4568744122@gmail.com")); // real recipient
messageTLS.setSubject("Test mail using TLS");
messageTLS.setText("This is test email sent to Your account using TLS.");
Transport transportTLS = sessionTLS.getTransport();
transportTLS.connect("smtp.gmail.com", 587, "random_sender_4568744122@gmail.com", "sender_account_pass"); // account used
transportTLS.sendMessage(messageTLS, messageTLS.getAllRecipients());
transportTLS.close();</span></pre>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><br /></span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><b>b)</b> using SSL connection to SMTP server:</span></div>
<pre class="prettyprint lang-java" style="font-family: Verdana,sans-serif;"><span style="font-family: inherit;">// ...
import java.util.Properties;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
// ...
Properties propsSSL = new Properties();
propsSSL.put("mail.transport.protocol", "smtps");
propsSSL.put("mail.smtps.host", "smtp.gmail.com");
propsSSL.put("mail.smtps.auth", "true");
Session sessionSSL = Session.getInstance(propsSSL);
sessionSSL.setDebug(true);
Message messageSSL = new MimeMessage(sessionSSL);
messageSSL.setFrom(new InternetAddress("random_sender_4568744122@gmail.com", "John Smith"));
messageSSL.setRecipients(Message.RecipientType.TO, InternetAddress.parse("random_recipient_4568744122@gmail.com")); // real recipient
messageSSL.setSubject("Test mail using SSL");
messageSSL.setText("This is test email sent to Your account using SSL.");
Transport transportSSL = sessionSSL.getTransport();
transportSSL.connect("smtp.gmail.com", 465, "random_sender_4568744122@gmail.com", "sender_account_pass"); // account used
transportSSL.sendMessage(messageSSL, messageSSL.getAllRecipients());
transportSSL.close();</span></pre>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><br /></span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><b>Note: </b>in both cases You for the connect() method, You have to pass a real login and password of the Google Mail account used. The main differences between those two methods are: different protocol (smtp for TLS and smtps for SSL), different ports (587 for TLS and 465 for SSL). In addition TLS requires STARTTLS.</span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><br /></span></div>
<div style="color: red;">
<span style="font-family: inherit;">------------------------------------------- </span></div>
<div style="color: red;">
<span style="font-family: inherit;">Download source files:</span><br />
<span style="font-family: inherit;"><br /></span></div>
<b>Note:</b> make sure that <a href="http://technology-for-human.blogspot.com/2009/04/instalacja-java-jdk-prawie-bezbolesna.html">Java</a> and <a href="http://technology-for-human.blogspot.com/2009/04/instalacja-i-konfiguracja-ide-eclipse.html">Eclipse</a> are properly installed and configured for running the project (additional configuration may be required if different directories are used). <br /><br /><br /><span style="font-family: inherit;"><span style="font-family: Verdana,sans-serif;">Eclipse complete sample project is </span><a href="https://sites.google.com/site/technologyforhuman/GoogleJavaMail.zip" style="font-family: Verdana,sans-serif;">here</a><span style="font-family: Verdana,sans-serif;"> (with all required libraries). </span></span>Paweł Nieściorukhttp://www.blogger.com/profile/12640446237233312925noreply@blogger.com5tag:blogger.com,1999:blog-160318223199333541.post-42913594023034191742011-08-03T23:38:00.001+02:002017-02-10T10:03:15.392+01:00SSL in Tomcat under Eclipse (part 2 - certificate from CA)<div style="font-family: Verdana,sans-serif;">
<b>Requirements:</b></div>
<ul style="font-family: Verdana,sans-serif;">
<li>You should be able to generate self signed SSL certificate and integrate it into Tomcat, as described in <a href="http://technology-for-human.blogspot.com/2011/08/ssl-in-tomcat-under-eclipse-part-1-self.html">previous post</a>.</li>
</ul>
<div style="font-family: Verdana,sans-serif;">
<b>You will learn:</b></div>
<ul style="font-family: Verdana,sans-serif;">
<li> how to obtain and install a real SSL certificate from well known Certificate Authority (CA)</li>
</ul>
<div style="font-family: Verdana,sans-serif;">
In the previous post I described the complete procedure of generating self signed SSL certificate and integrating it into Tomcat. In this post I would like to focus on the example with real certificate obtained from CA - I will use Thawte as example.</div>
<div style="font-family: Verdana,sans-serif;">
<br /></div>
<div style="font-family: Verdana,sans-serif;">
<b>Note:</b> let's assume that You created an application which is going to be visible under the following URL: <b>http://www.myapp.com</b>.</div>
<div style="font-family: Verdana,sans-serif;">
<br /></div>
<div style="font-family: Verdana,sans-serif;">
<b>Step 1:</b> generating self signed certificate for domain www.myapp.com.</div>
<div style="font-family: Verdana,sans-serif;">
<br /></div>
<div style="font-family: Verdana,sans-serif;">
This is exactly the same step to step 1 in <a href="http://technology-for-human.blogspot.com/2011/08/ssl-in-tomcat-under-eclipse-part-1-self.html">previous post</a>. You have to execute the following command:</div>
<div style="font-family: Verdana,sans-serif;">
<br /></div>
<div style="font-family: "Courier New",Courier,monospace;">
keytool -genkey -alias myappcert -keyalg RSA -keystore myapp.keystore</div>
<div style="font-family: Verdana,sans-serif;">
<br /></div>
<div style="font-family: Verdana,sans-serif;">
<b>Step 2:</b> Generate Certificate Signing Request (CSR).</div>
<div style="font-family: Verdana,sans-serif;">
<br /></div>
<div style="font-family: Verdana,sans-serif;">
You have to generate a special request, which will be send to the CA. You have to execute command:</div>
<div style="font-family: Verdana,sans-serif;">
<br /></div>
<div style="font-family: "Courier New",Courier,monospace;">
keytool -certreq -keyalg RSA -alias myappcert -file certreq.csr -keystore myapp.keystore</div>
<div style="font-family: Verdana,sans-serif;">
<br /></div>
<div style="font-family: Verdana,sans-serif;">
Generated request is saved as a certreq.csr file, which will be send to CA. CA will use this file to generate certificate signed by them. </div>
<div style="font-family: Verdana,sans-serif;">
<br /></div>
<div style="font-family: Verdana,sans-serif;">
<b style="color: red;">Important:</b> You have to use exactly the same alias (in this example: myappcert) for step 1 and step 2.</div>
<div style="font-family: Verdana,sans-serif;">
<br /></div>
<div style="font-family: Verdana,sans-serif;">
<b>Step 3:</b> Getting certificate from CA.</div>
<div style="font-family: Verdana,sans-serif;">
<br /></div>
<div style="font-family: Verdana,sans-serif;">
Usually certificate are delivered in <b>PKCS#7</b> or <b>X.509</b> format. For the first one, the file with certificate will have <b>.p7b</b> extension, for the second one - <b>.cer</b>. Sometimes You can get such certificate <b>also by email as a pure text</b> - for the X.509 format, certificate will be placed between tags <b>-----BEGIN CERTIFICATE-----</b> and <b>-----END CERTIFICATE-----</b>, for PKCS#7 certificate will be placed between tags <b>-----BEGIN PKCS7-----</b> and <b>-----END PKCS7-----</b>. Then You have to copy the certificate content (including those begin/end tags!) and save this as a .cer or .p7b file (You can use Notepad for that). Now You are ready to import Your signed certificate.</div>
<div style="font-family: Verdana,sans-serif;">
<br /></div>
<div style="font-family: Verdana,sans-serif;">
<b>Step 4: </b>importing signed certificate.</div>
<div style="font-family: Verdana,sans-serif;">
<br /></div>
<div style="font-family: Verdana,sans-serif;">
<b>a) as a .p7b (PKCS#7) format:</b></div>
<div style="font-family: Verdana,sans-serif;">
Acording to the Thawte, when You obtained Your certificate as a .p7b file, You need only one command to import and install this certificate in Your keystore:</div>
<div style="font-family: Verdana,sans-serif;">
<br /></div>
<div style="font-family: Verdana,sans-serif;">
</div>
<div style="font-family: "Courier New",Courier,monospace;">
keytool -import -alias myappcert -trustcacerts -file signed_cert.p7b -keystore myapp.keystore</div>
<div style="font-family: Verdana,sans-serif;">
<br /></div>
<div style="font-family: Verdana,sans-serif;">
where <span style="font-family: "courier new" , "courier" , monospace;">signed_cert.p7b</span> is a signed certificate obtained from CA.</div>
<div style="font-family: Verdana,sans-serif;">
<br /></div>
<div style="font-family: Verdana,sans-serif;">
<b>b) as a .cer (X.509) format:</b></div>
<div style="font-family: Verdana,sans-serif;">
According to the Thawte, when You obtained Your certificate as .cer file, You need to download Primary and Secondary Intermediate CAs and import them. They are delivered as .p7b format (i.e. intermediate.p7b). I should import them first, using command:</div>
<div style="font-family: Verdana,sans-serif;">
<br /></div>
<div style="font-family: "Courier New",Courier,monospace;">
keytool -import -trustcacerts -alias intermediatecerts -file intermediate.p7b -keystore myapp.keystore</div>
<div style="font-family: Verdana,sans-serif;">
<br /></div>
<div style="font-family: Verdana,sans-serif;">
and then import my certificate (it is in .cer file signed_cert.cer) using this command:</div>
<div style="font-family: Verdana,sans-serif;">
</div>
<div style="font-family: Verdana,sans-serif;">
<br /></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: "courier new" , "courier" , monospace;">keytool -import -trustcacerts -alias myappcert -file signed_cert.cer -keystore myapp.keystore</span><b></b></div>
<div style="font-family: Verdana,sans-serif;">
<b><br />
</b></div>
<div style="font-family: Verdana,sans-serif;">
where <span style="font-family: "courier new" , "courier" , monospace;">signed_cert.cer</span> is a signed certificate obtained from CA.</div>
<div style="font-family: Verdana,sans-serif;">
<br /></div>
<div style="font-family: Verdana,sans-serif;">
</div>
<div style="font-family: Verdana,sans-serif;">
<b>Problem and workaround: </b></div>
<div style="font-family: Verdana,sans-serif;">
Both above ways seems to be simple, but... when I was buying some time ago a web SSL certificate from Thawte, I receive certificate as a pure text in e-mail. Certificate was in X.509 format (I had tags ----BEGIN/END CERTIFICATE-----). I created a <span style="font-family: "courier new" , "courier" , monospace;">signed_cert.cer</span> file from that e-mail content. Then I tried to install intermediate certificates like it was described in Thawte documentation.<b> Unfortunately it didn't work</b>. The first command for importing intermediate certificates failed with error:</div>
<div style="font-family: Verdana,sans-serif;">
<br /></div>
<div style="font-family: "Courier New",Courier,monospace;">
keytool error: java.lang.Exception: Input not an X.509 certificate</div>
<div style="font-family: Verdana,sans-serif;">
<br /></div>
<div style="font-family: Verdana,sans-serif;">
It seems that <b>keytool does not work with .p7b format</b> (I used JDK 1.6.0_16), so I expect that even if I get my certificate as .p7b file (complete, without need to import intermediate certificates as in a) subpoint) it will also not work. I took a .p7b file with intermediate certificates, opened it under Windows, and for each certificate found inside i exported it as a X.509 DER certificate, giving each file .cer extension. So I had three .cer (X.509) files: signed_cert.cer, thawte_primary.cer, thawte_secondary.cer. Now I had to import intermediate certificates and after that import my signed certificate with those commands:</div>
<div style="font-family: Verdana,sans-serif;">
</div>
<div style="font-family: Verdana,sans-serif;">
<br />
<span style="font-family: "courier new" , "courier" , monospace;">keytool -import -trustcacerts -alias primary -file thawte_primary.cer -keystore myapp.keystore</span><br />
<span style="font-family: "courier new" , "courier" , monospace;">keytool -import -trustcacerts -alias secondary -file thawte_secondary.cer.cer -keystore myapp.keystore</span></div>
<div style="font-family: "Courier New",Courier,monospace;">
keytool -import -trustcacerts -alias myappcert -file signed_cert.cer -keystore myapp.keystore</div>
<div style="font-family: Verdana,sans-serif;">
<br /></div>
<div style="font-family: Verdana,sans-serif;">
Now import was OK. So the <b>general workaround rule</b> <b>(if there are any troubles) is:</b> get all needed certificates in X.509 form, import intermediate certificates (if any required) and import Your signed certificate at the end.</div>
<div style="font-family: Verdana,sans-serif;">
<br /></div>
<div style="font-family: Verdana,sans-serif;">
<b style="color: red;">Important:</b> You have to use exactly the same alias (in this example: myappcert) like You used in step 1 and step 2, except for importing intermediate certificates - You can use whatever alias You like. </div>
<div style="font-family: Verdana,sans-serif;">
<br /></div>
<div style="font-family: Verdana,sans-serif;">
<b>Step 5:</b> copy Your <span style="font-family: "courier new" , "courier" , monospace;">myapp.keystore</span> file into Tomcat's /conf directory</div>
<div style="font-family: Verdana,sans-serif;">
<br /></div>
<div style="font-family: Verdana,sans-serif;">
<b>Step 6:</b> modify Tomcat configuration to use SSL certificate</div>
<div style="font-family: Verdana,sans-serif;">
<br /></div>
<div style="font-family: Verdana,sans-serif;">
It is exactly the same as step 3 in <a href="http://technology-for-human.blogspot.com/2011/08/ssl-in-tomcat-under-eclipse-part-1-self.html">previous post</a>.</div>
<div style="font-family: Verdana,sans-serif;">
<br /></div>
<div style="font-family: Verdana,sans-serif;">
<br /></div>
<div style="font-family: Verdana,sans-serif;">
That's all.</div>
Paweł Nieściorukhttp://www.blogger.com/profile/12640446237233312925noreply@blogger.com0tag:blogger.com,1999:blog-160318223199333541.post-69423875468999164762011-08-01T23:21:00.001+02:002018-12-16T14:12:49.765+01:00SSL in Tomcat under Eclipse (part 1 - self signed certificate)<div style="font-family: Verdana,sans-serif;">
<b><span style="font-family: inherit;">Requirements:</span></b></div>
<ul style="font-family: Verdana,sans-serif;">
<li><span style="font-family: inherit;">installed Java (description <a href="http://technology-for-human.blogspot.com/2009/04/instalacja-java-jdk-prawie-bezbolesna.html">here</a>)</span></li>
<li><span style="font-family: inherit;">installed and configured Eclipse (description <a href="http://technology-for-human.blogspot.com/2009/04/instalacja-i-konfiguracja-ide-eclipse.html">here</a>)</span></li>
<li><span style="font-family: inherit;">installed and configured Tomcat for the Eclipse (description <a href="http://technology-for-human.blogspot.com/2009/05/konfiguracja-tomcata-w-eclipse.html">here</a>)</span></li>
</ul>
<div style="font-family: Verdana,sans-serif;">
<b><span style="font-family: inherit;">You will learn:</span></b></div>
<ul style="font-family: Verdana,sans-serif;">
<li><span style="font-family: inherit;"> how to set up Tomcat for SSL connection using self signed certificate under Eclipse</span></li>
</ul>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;">When You want to secure Your application SSL is the most natural choice. In order to make Your application be recognized as trusted by browser, You can by a buy a certificate from well known certificate authority (CA), which will be generated for the domain Your application uses. If You do not care about being recognized as trusted service, but instead You just want to encrypt the data exchanged between server and client's browser, You can use Your own self signed SSL certificate. In this post I will show how to generate such SSL certificate and how to set up Tomcat (from the Eclipse level) to use generated certificate.</span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><br /></span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><b>Note:</b> let's assume that You created an application which is going to be visible under the following URL: <b>http://www.myapp.com</b>.</span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><br /></span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><b>Step 1:</b> generating self signed certificate for domain www.myapp.com.</span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><br /></span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;">Go into Your Java bin directory (i.e <span style="font-family: "Courier New",Courier,monospace;">C:\Development\Java\bin</span>). Then open Windows console (under Windows Vista/7 open the console with Administrator right) and type in command:</span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><br /></span></div>
<div style="font-family: "Courier New",Courier,monospace;">
<span style="font-family: inherit;">keytool -genkey -alias myappcert -keyalg RSA -keystore myapp.keystore </span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><br /></span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;">where <span style="font-family: "Courier New",Courier,monospace;">myappcert</span> is the name of the certificate being generated and <span style="font-family: "Courier New",Courier,monospace;">myapp.keystore</span> is a file where certificate will be stored. You will be asked about the password for created the myapp.keystore file. Type in "mypass", press Enter and type it again and press Enter again. Then You will be asked for some details about You:</span></div>
<div style="font-family: Verdana,sans-serif;">
</div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><span style="font-family: inherit;"><br /></span>
</span><br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi4XoggrtA_rZA_yF3tjRDhX7_tvonY0g2flocl_kthgOJ3U774UOxQlUDTTSiEKUOv8YPVCo3tCowneDHn9pWxcqs70JxUxbfM3jTBOpw_n5fbjLnz2j2KdT5hQKYNIoCGICqrtFwpxofU/s1600/2011.08.01_1.PNG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><span style="font-family: inherit;"><img border="0" height="226" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi4XoggrtA_rZA_yF3tjRDhX7_tvonY0g2flocl_kthgOJ3U774UOxQlUDTTSiEKUOv8YPVCo3tCowneDHn9pWxcqs70JxUxbfM3jTBOpw_n5fbjLnz2j2KdT5hQKYNIoCGICqrtFwpxofU/s400/2011.08.01_1.PNG" width="400" /></span></a></div>
</div>
<div class="separator" style="clear: both; text-align: center;">
</div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><br /></span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;">Please note that for the first question about first and last name I gave answer www.myapp.com. <b>This is very important</b> - this name (known as CN - Common Name) will be used for the checking if certificate on the page we visit was generated for the same URL which we typed in in the browser.</span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;">At the end You will be asked for the password for the newly created certificate. The password must be the same as one used for <span style="font-family: "Courier New",Courier,monospace;">myapp.keystore</span> file ("mypass"). Do not type antyhing, just press Enter to use the same password. Your myapp.keystore file containing myappcert certificate is ready.</span></div>
<div style="font-family: Verdana,sans-serif;">
<b><span style="font-family: inherit;"><br />
</span></b></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><b>Step 2:</b> copy Your <span style="font-family: "Courier New",Courier,monospace;">myapp.keystore</span> file into Tomcat's <span style="font-family: "Courier New",Courier,monospace;">/conf</span> directory</span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><br /></span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><b>Step 3:</b> modify Tomcat configuration to use SSL certificate.</span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><br /></span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;">Make sure that Your Tomcat is configured with Eclipse and works OK without SSL (start Tomcat from Eclipse and type in <span style="font-family: "Courier New",Courier,monospace;">http://localhost:8080</span> in the browser). Stop Tomcat if it is running. Then open <span style="font-family: "Courier New",Courier,monospace;">server.xml</span> file form the "Servers" view:</span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><span style="font-family: inherit;"><br /></span>
</span><br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjnO2n2Av6BdSshh_ZkoD79PTk7C1VnGI1iCBRMKDWvpw5vYv21UX19H5-OukdSEcpBtC1mM52TZYY5-NmYkMoeFbsMykrmGhGaIe9aOHKJSeYGlLlcdslIdJWdMH-A1_jSdo__KmlBr6V6/s1600/2011.08.01_2.PNG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><span style="font-family: inherit;"><img border="0" height="168" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjnO2n2Av6BdSshh_ZkoD79PTk7C1VnGI1iCBRMKDWvpw5vYv21UX19H5-OukdSEcpBtC1mM52TZYY5-NmYkMoeFbsMykrmGhGaIe9aOHKJSeYGlLlcdslIdJWdMH-A1_jSdo__KmlBr6V6/s400/2011.08.01_2.PNG" width="400" /></span></a></div>
<span style="font-family: inherit;"><br /></span></div>
<div class="separator" style="clear: both; text-align: center;">
</div>
<div style="font-family: Verdana,sans-serif;">
</div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;">and locate default <connector> element for standard HTTP connections (marked red). Then add additional <connector> element for the SSL connection (next to existing one):</connector></connector></span></div>
<pre class="prettyprint lang-xml" style="font-family: Verdana,sans-serif;"><span style="font-family: inherit;"><Connector
SSLEnabled="true"
clientAuth="false"
keyAlias="myappcert"
keystoreFile="conf/myapp.keystore"
keystorePass="mypass"
maxThreads="200"
port="8081"
scheme="https"
secure="true"
sslProtocol="TLS"
/> </span></pre>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><br /></span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;">Please note that next to some specific SSL settings, we set the location of the keystore file, the password for that file and certificate name to be use. SSL connection uses port 8081, where normal HTTP connection </span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;">uses port 8080.</span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><br /></span></div>
<div style="font-family: Verdana,sans-serif;">
</div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><b>Note 1:</b> all modification of <span style="font-family: "Courier New",Courier,monospace;">server.xml</span> file were done from Eclipse level, using "Servers" view. If You want to use Eclipse WTP for starting Tomcat (like I do so far) You <b>can't edit this file from elsewhere</b>. Eclipse WTP overrides original Tomcat configuration files by files visible under "Servers" - changes done outside Eclipse <b>will not be visible for WTP</b>.</span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><br /></span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><b>Note 2:</b> I added <span style="font-family: "Courier New",Courier,monospace;"><connector></connector></span> element specific for SSL next to existing <span style="font-family: "Courier New",Courier,monospace;"><connector></connector></span> element for standard non encrypted HTTPS connections. This is second issue directly connected with Eclipse WTP - <b>when You remove standard connector for HTTP, Eclipse will close Tomcat</b> after a time set in "Timeouts" section in the Tomcat settings:</span><br />
<span style="font-family: inherit;"><span style="font-family: inherit;"><br /></span>
</span><br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEipn3AAqFFDEiaUEW0SDxQTYe8f3oQX20fRiYCsEBkAVgqAJHXgWpWsL9DPtE-MoMeDbvf8zdxHc6EULP3DyUxaKdAtKS-M83kwAfDzgDniyG3bIzoqMFMdQZz57GAxrCEn7BH0rm9GjuZc/s1600/2011.08.01_3.PNG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><span style="font-family: inherit;"><img border="0" height="238" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEipn3AAqFFDEiaUEW0SDxQTYe8f3oQX20fRiYCsEBkAVgqAJHXgWpWsL9DPtE-MoMeDbvf8zdxHc6EULP3DyUxaKdAtKS-M83kwAfDzgDniyG3bIzoqMFMdQZz57GAxrCEn7BH0rm9GjuZc/s400/2011.08.01_3.PNG" width="400" /></span></a></div>
<span style="font-family: inherit;"><br /></span></div>
<div style="font-family: Verdana,sans-serif;">
</div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;">Some people try to extend the timeout time into long period, but still after this period Eclipse kills Tomcat process, as if Tomcat was not properly started in the required time. Unfortunately there is no way to set timeout into ifinite time - the only way to fix this under WTP is to leave standard HTTP <span style="font-family: "Courier New",Courier,monospace;"><connector></connector></span> element in Tomcat's <span style="font-family: "Courier New",Courier,monospace;">server.xml</span> file.</span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><br /></span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><b>Step 4:</b> that's all. Tomcat is configured to work with SSL. </span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><br /></span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;">Try to enter the URL: <span style="font-family: "Courier New",Courier,monospace;">https://localhost:8081</span>. If You see Tomcat's page, everything works OK (You can see certificate warning - You Need to add security exception for that certificate). Of course You can still open the same Tomcat page by standard HTTP (You have two <span style="font-family: "Courier New",Courier,monospace;"><connector></connector></span> elements), just enter <span style="font-family: "Courier New",Courier,monospace;">http://localhost:8080</span> URL.</span></div>
Paweł Nieściorukhttp://www.blogger.com/profile/12640446237233312925noreply@blogger.com2tag:blogger.com,1999:blog-160318223199333541.post-41419424393595713622011-07-27T19:52:00.004+02:002017-02-10T18:02:48.769+01:00Tomcat OutOfMemoryError: Heap space/PermGen space<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><b>Note: </b>below description uses <b>Tomcat 7.0.28</b></span><br />
<span style="font-family: inherit;"><br /></span>
<span style="font-family: inherit;">Have You ever seen such error in Your logs? Probably yes. It simply means that available memory for Tomcat was consumed and nothing left. To be precise: <b>available memory for JVM which Tomcat uses to run</b>.</span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><br /></span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;">What causes this error? It can be caused by memory leaks in the application or the applications has big requirements "by design", even if there are no memory leaks. For the first case You should use some profiling tools to find and fix memory leaks - perhaps this might help without need to change memory settings for Tomcat JVM. If You are sure that Your application has no memory leaks, the only way is to increase memory used by Tomcat JVM. See below how to do that - please note that I described modifying <b>Tomcat memory settings when it is installed as a service under Windows OS</b>.</span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><br /></span></div>
<div style="font-family: Verdana,sans-serif;">
<b><span style="font-family: inherit;">32-bit Windows</span></b></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;">The main problem here is that 32-bit OS is able to see no more than 3,2GB of RAM, even if You have 4GB or more physically installed. This is upper limit of memory that can be use - in theory. However, in practise You will not be able to use more than 1 to 1,5GB of RAM for Tomcat's JVM - the rest of memory is used by OS itself and installed software.</span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><br /></span></div>
<div style="font-family: Verdana,sans-serif;">
<b><span style="font-family: inherit;">64-bit Windows</span></b></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;">Let's assume that You need to assign more than 1,5GB of RAM for Tomcat JVM. Therefore You need 64-bit OS with at least 4GB<b> </b>of RAM (for example 64-bit Windows Professional supports up to 192GB of RAM). Of course You must also use 64-bit JDK - when You use 32-bit JDK under 64-bit OS, You will again face the limit of 3,2GB RAM. You also must use 64-bit version of Tomcat (as it contains Windows service wrapper to use with 64-bit JVMs on 64-bit Windows platforms). Below You will find complete list of all needed steps:</span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><br /></span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><b>Step 1:</b> make sure You are using 64-bit Windows OS.<b> </b></span><br />
<span style="font-family: inherit;"><br /></span>
<span style="font-family: inherit;"><b>Step 2:</b> install 64-bit JDK and set $JAVA_HOME to the installation directory.<b> </b></span><br />
<span style="font-family: inherit;"><br /></span>
<span style="font-family: inherit;"><b>Step 3:</b> download .zip file with 64-bit Tomcat for Windows (file: <span style="font-family: "Courier New",Courier,monospace;">apache-tomcat-[version]-windows-x64.zip</span>).<b> </b></span><br />
<span style="font-family: inherit;"><br /></span>
<span style="font-family: inherit;"><b>Step 4:</b> extract Tomcat and go into Tomcat's <span style="font-family: "Courier New",Courier,monospace;">/bin</span> directory.</span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><br /></span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><b>Step 5:</b> open <span style="font-family: "Courier New",Courier,monospace;">service.bat</span> file and locate the line with <span style="font-family: "Courier New",Courier,monospace;">--JvmMS</span> and <span style="font-family: "Courier New",Courier,monospace;">--JvmMX </span>parameters, and modify it with new values memory, eg:</span><br />
<span style="font-family: inherit;"><br /></span>
<span style="font-family: inherit;">%EXECUTABLE%" //US//%SERVICE_NAME% ++JvmOptions "-Djava.io.tmpdir=%CATALINA_BASE%\temp" ++JvmOptions "-XX:MaxPermSize=1024m" --JvmMs 2048 --JvmMx 4096</span><br />
<span style="font-family: inherit;"><br /></span>
<span style="font-family: inherit;"><b>Note:</b> <span style="font-family: "Courier New",Courier,monospace;">PermSize</span> is set in different way than heap which has predefined <span style="font-family: "Courier New",Courier,monospace;">JvmMS</span> and <span style="font-family: "Courier New",Courier,monospace;">JvmMX</span> flags.</span><br />
<span style="font-family: inherit;"><br /></span>
<span style="font-family: inherit;"><b>Step 6:</b> open Windows console with administrator rights, then go into Tomcat /bin and execute command:</span><br />
<span style="font-family: inherit;"><br /></span>
<span style="font-family: inherit;">service.bat install tomcat6</span><br />
<span style="font-family: inherit;"><br /></span>
<span style="font-family: inherit;"><b>Step 7:</b> start service with command:</span><br />
<span style="font-family: inherit;"><br /></span>
<span style="font-family: inherit;">net start tomcat6</span><br />
<span style="font-family: inherit;"><br /></span>
<span style="font-family: inherit;"><b>Note 1:</b> You can skip point 5 and execute directly point 6. After that just start <span style="font-family: "Courier New",Courier,monospace;">tomcat6w.exe</span> which is a GUI tool for managing the service. You can set memory values there.</span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><br /></span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><b>Note 2:</b> as an alternative to above steps, You can download Tomcat service installer (file: <span style="font-family: "Courier New",Courier,monospace;">apache-tomcat-[version].exe</span>) which performs service installation, and then use <span style="font-family: "Courier New",Courier,monospace;">tomcat6w.exe</span> tool for tune memory settings.</span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><br /></span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;">That's all. Your Tomcat service should use provided memory settings. </span></div>
Paweł Nieściorukhttp://www.blogger.com/profile/12640446237233312925noreply@blogger.com0tag:blogger.com,1999:blog-160318223199333541.post-12115573634744280272011-05-22T17:41:00.005+02:002017-02-10T18:01:17.402+01:00PostgreSQL - strange database server errors under Windows<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;">I often use PostgreSQL as a database server for my web applications which are usually deployed under Tomcat. PostgreSQL and Tomcat are registered as system services which starts automatically when Windows starts. </span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><br /></span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;">In one of my application I have about 50 different databases with connection pools for each database, where each pool has about 10 connections opened. So I have about 500 connections opened for requests. Each opened connection is visible as a separate postgres.exe process in the system next to the own PostgreSQL processes also visible as postgres.exe (usually 5 processes). Sometimes <b>under Windows XP</b> <b>Prof. SP 3</b> I saw PostgreSQL stop working and in the server logs there were errors like: </span></div>
<ul>
<li><span style="font-family: inherit;"><b></b> <span style="font-family: "Courier New",Courier,monospace;">java.net.SocketException: No buffer space available (maximum connections reached?): recv failed </span></span></li>
</ul>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;">or: </span></div>
<ul>
<li><span style="font-family: inherit;"><span style="font-family: inherit;"><span style="font-family: "Courier New",Courier,monospace;">server process (PID XXXX) was terminated by exception 0xC0000142 HINT: See C include file "ntstatus.h" for a description of the hexadecimal value.</span></span> </span></li>
</ul>
<div style="color: red;">
<span style="font-family: inherit;">--------------------------------------------</span></div>
<div style="color: red;">
<span style="font-family: inherit;">No buffer space available error:</span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><br /></span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;">"Maximum connection reached" may suggest that we set too low value of <span style="font-family: "Courier New",Courier,monospace;">max_connections</span> in <span style="font-family: "Courier New",Courier,monospace;">postgresql.conf</span> file. Let's assume that settings for connections amount and required memory are OK for the server, but the problem is still visible from time to time.</span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><br /></span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;">The first thing to be checked is amount of short-lived ports (ephemeral). Those ports are used by applications during their normal work. They are opened and live until certain application works, in comparison to server application ports that are typically open for the entire time that the server computer is running. They are assigned automatically from predefined range. Range differs between operating systems:</span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><br /></span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;">a) Windows 2000 -> from 1024 to 5000</span><br />
<span style="font-family: inherit;">b) Windows 2003 Server -> from 1024 to 5000</span><br />
<span style="font-family: inherit;">c) Windows XP (all service packs) -> from 1024 to 5000</span><br />
<span style="font-family: inherit;">d) Windows Vista -> from 49152 to 65535</span><br />
<span style="font-family: inherit;">e) Windows 2008 Server -> from 49152 to 65535</span><br />
<span style="font-family: inherit;">f) Windows 7 -> from 49152 to 65535 </span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;">g) Linux kernels -> from 32768 to 61000 (see /proc/sys/net/ipv4/ip_local_port_range)</span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><br /></span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;">If the available pool of ephemeral ports is exahsuted we see Windows WSAENOBUFS error (no buffer space, code 10055). In order to check for exceeded range, we have to see how many processes are running in the system and how many ports they are opening - we can use free <a href="http://technet.microsoft.com/en-us/sysinternals/bb897437">TCPView</a> application. We can kill the process that causes exceeding or we can increase the range of ephemeral ports. If we want to increase it, we have to change a registry entry named maxUserPort in:</span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><br /></span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;">HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\maxUserPort</span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><span style="font-family: "Courier New",Courier,monospace;"> </span> </span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;">If that registry entry does not exist it means that system uses default value (see above) and we have to create this entry. Then we have to set a proper value for that entry (REG_DWORD) from the range 5000 to 65534. </span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><br /></span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;">The second thing to be checked is limit of half-opened TCP/IP connections (connection with state SYN-RCVD). When this limit is exceeded, TCP/IP starts up SYN flooding attack protection (when SynAttackProtect is enabled). This limit differs between operating systems:</span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><br /></span>
<span style="font-family: inherit;">a) Windows 2000 Workstation -> 500</span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;">b) Windows 2000 Server -> 100</span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;">c) Windows Server 2003 -> 500 </span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;">d) Windows XP SP 1 -> 100 </span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;">e) Windows XP SP 2, SP3 -> 10 </span></div>
<div style="font-family: Verdana,sans-serif;">
</div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;">f) Windows Vista SP 1 -> 10</span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;">g) Windows Vista SP 2 -> limit removed (not implmented = unlimited)</span><br />
<span style="font-family: inherit;">h) Windows Server 2008 SP 1 -> 10</span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;">i) Windows Server 2008 SP 2 -> limit removed (not implemented = unlimited)</span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;">f) Windows 7 -> limit removed (not implemented = unlimited) </span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><br /></span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;">We can use mentioned TCPView application to check for half-opened TCP connections. This limit can be increased by changing the registry entry named TcpMaxHalfOpen in:</span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><br /></span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;">HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\TcpMaxHalfOpen</span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><br /></span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;">If that registry entry does not exist it means that system uses default value (see above) and we have to create this entry and then manualy assign new value.</span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><br /></span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;">Under Windows Vista SP2, Windows 7 or Linux I did not see that error. </span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><br /></span></div>
<div style="color: red;">
<span style="font-family: inherit;">--------------------------------------------</span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><span style="color: red; font-family: Verdana,sans-serif;">0xC0000142 error</span><span style="color: red;">: </span></span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><br /></span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;">This problem is not so easy to explain, although I was able to fix it. The error code means that "A dynamic link library (DLL) initialization routine failed." I am not sure what exacly causes this... </span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><br /></span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;">In my case under <b>Windows XP Prof. SP 3</b>, when I started my application and number of postgres.exe processes was about 100 (it should be about 500, see description at the top) the whole database server process was suddenly terminated and I saw second mentioned error (with error code <span style="font-family: inherit;"><span style="font-family: "Courier New",Courier,monospace;">0xC0000142</span></span>) in the logs. The most important part here is that PostgreSQL service, which spawned many postgres.exe child processes, was <b>registered under LOCAL SYSTEM</b> account, <b>without</b> "Allow Service to Interact with Desktop" option set.</span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><br /></span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;">It seems that this problem is not only connected with PostgreSQL. It is general problem with services and their subprocesses (see <a href="http://stefan.samaflost.de/blog/en/Windows">this </a>and <a href="http://www.eggheadcafe.com/microsoft/Windows-Server/29929112/custom-services-failing-on-restart-when-70-are-running.aspx">that</a> article). From the information found the problem my be connected with so-called desktop heaps (see more <a href="http://support.microsoft.com/default.aspx?scid=kb;[LN];184802">here</a> and <a href="http://blogs.msdn.com/b/ntdebugging/archive/2007/01/04/desktop-heap-overview.aspx">here</a>). Some people having similar problems with many subprocesses did the workaround by setting "Allow Service to Interact with Desktop" to true for their LOCAL SYSTEM services. I was able to fix it under Windows XP Prof. SP 3, by <b>registering PostgreSQL service for regular user account instead of LOCAL SYSTEM</b> (see "Long Version" from my article about <a href="http://technology-for-human.blogspot.com/2009/04/instalacja-postgresql-hard.html">manual installation of PostgreSQL</a> how to do this). I also checked this under Windows Vista SP2 and Windows 7 - it works OK there, even under LOCAL SYSTEM account. Under Linux I did not see that error.</span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><br /></span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;">I <b>did not try</b> to assign more desktop heap from default for Windows XP SharedSection=1024,3072,512 (2nd or 3rd value needs to be changed) and then try it again to run under LOCAL SYSTEM without "Allow Service to Interact with Desktop". Perhaps this might also help.</span></div>
Paweł Nieściorukhttp://www.blogger.com/profile/12640446237233312925noreply@blogger.com1tag:blogger.com,1999:blog-160318223199333541.post-91632231236230234962011-04-26T19:36:00.002+02:002018-12-16T15:21:41.783+01:00JSF 2, Spring 3, JPA (Hibernate 3), PostgreSQL, c3p0 - everything together (part 2 of 2)<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><b>Note: </b>below description uses <b>Eclipse Indigo</b>, <b>Tomcat 7.0.28</b>,<b> MyFaces 2.1.7</b>, <b>Spring Framework 3.1.1</b>, <b>Spring Security 3.1.0</b>,<b> Hibernate 3.6.10 (Final)</b>,<b> </b><b>PostgreSQL 9.1</b>.<b> </b></span><br />
<span style="font-family: inherit;"><br /></span>
<b><span style="font-family: inherit;">Requirements:</span></b></div>
<ul style="font-family: Verdana,sans-serif;">
<li><span style="font-family: inherit;">running database and complete project with all required libraries integrated, described in previous post (can be found <a href="http://technology-for-human.blogspot.com/2011/03/jsf-2-spring-3-jpa-hibernate-3.html">here</a>)</span></li>
</ul>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><br /></span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><b>Step 4 (continued):</b> configuration files - <span style="font-family: "courier new" , "courier" , monospace;">faces-config.xml</span>.</span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><br /></span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;">Comparing to a JSF 2.0 project with Spring integrated, only <span style="font-family: "courier new" , "courier" , monospace;">applicationContext.xml</span> file will be changed - <span style="font-family: "courier new" , "courier" , monospace;">web.xml</span> and faces-config.xml files will be the same. We need to add below code into <span style="font-family: "courier new" , "courier" , monospace;">applicationContext.xml</span>:</span></div>
<pre class="prettyprint lang-xml"><span style="font-family: inherit;"><bean id="pooledDataSource"
class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass" value="org.postgresql.Driver" />
<property name="jdbcUrl" value="jdbc:postgresql://localhost:5432/bikes"/>
<property name="user" value="postgres"/>
<property name="password" value="pgpass"/>
<property name="maxPoolSize" value="10" />
<property name="maxStatements" value="0" />
<property name="minPoolSize" value="5" />
</bean>
<bean id="JDBCDataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource" >
<property name="driverClassName" value="org.postgresql.Driver"/>
<property name="url" value="jdbc:postgresql://localhost:5432/bikes"/>
<property name="username" value="postgres"/>
<property name="password" value="pgpass"/>
</bean>
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" >
<!--
Specific properties for Hibernate are in persistence.xml file,
but also can be placed here and removed from persistence.xml file.
-->
</bean>
</property>
<property name="dataSource" ref="pooledDataSource" />
<property name="persistenceUnitName" value="persistenceUnit"/>
</bean>
<bean
class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
<bean name="transactionManager"
class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<tx:annotation-driven /></span></pre>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;">Note that this approach is similar to situation when plain Hibernate API is used with Spring: JPA's EntityManagerFactory is like Hibernate's SessionFactory, JPA's EntityManager (obtained from EntityManagerFactory) is like Hibernate's Session (obtained from SessionFactory). JPA's EntityManager is injected into Repositories (with the help of Spring's PersistenceAnnotationBeanPostProcessor class), while Hibernate's Session was injected into DAO's (with the help of Spring's HibernateDaoSupport class exteneded by each DAO). JPA's TransactionManager (built on EntityManagerFactory) is like Hibernate's HibernateTransactionManager (built on SessionFactory). </span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;">JPA's Repositories and Services are similar to oldschool DAOs and Facades. Repositories are injected into Services, while DAOs are injected into Facades.</span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><br /></span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;">Please note that I defined here two datasources: a basic one without connection pool (<span style="font-family: "courier new" , "courier" , monospace;">JDBCDataSource</span>) and second one using c3p0 as a connection pool (<span style="font-family: "courier new" , "courier" , monospace;">pooledDataSource</span>).</span></div>
<span style="font-family: inherit;"><br /></span>
<br />
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><b>Step 5:</b> configuration files - <span style="font-family: "courier new" , "courier" , monospace;">persistence.xml</span></span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><br /></span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;">In addition JPA uses a special configuration file named persistence.xml. This file contains additional settings for JPA vendor. The location of this file is described in this <a href="http://javahowto.blogspot.com/2007/06/where-to-put-persistencexml-in-web-app.html">article</a>. In order to have this file inside <span style="font-family: "courier new" , "courier" , monospace;">WEB-INF/classes/META-INF/persistence.xml</span> under Eclipse, I created a directory "config" where I put META-INF subdirectory with <span style="font-family: "courier new" , "courier" , monospace;">persistence.xml</span> file inside. Then I select the directory "config" to be used as a source directory. This means that content of "config" directory will be on classpath, and for a web application during a build time it will be moved into <span style="font-family: "courier new" , "courier" , monospace;">WEB-INF/classes</span>. Note that I also added there log4j config file:</span><br />
<span style="font-family: inherit;"><br /></span>
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg8-hw69ty7ngdZ7M3JdMt6xy-4k2qdKYxY4dpKBWmP_sdqzJcG9f5L7F5f3E2T_kGKdU91CahHls1Z1Ev-AAsVt6wgvgw0zvIqHAejPczuQITtSL6ar8yj0wD46y4NMR7YHxw3L1wq0vc6/s1600/2011.04.26_1.PNG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><span style="font-family: inherit;"><img border="0" height="190" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg8-hw69ty7ngdZ7M3JdMt6xy-4k2qdKYxY4dpKBWmP_sdqzJcG9f5L7F5f3E2T_kGKdU91CahHls1Z1Ev-AAsVt6wgvgw0zvIqHAejPczuQITtSL6ar8yj0wD46y4NMR7YHxw3L1wq0vc6/s400/2011.04.26_1.PNG" width="400" /></span></a></div>
</div>
<span style="font-family: inherit;"><br /></span>
<br />
<div class="separator" style="clear: both; text-align: center;">
</div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><span style="font-family: "courier new" , "courier" , monospace;">Persistence.xml</span> file contains some specific settings for Hibernate (as JPA vendor):</span></div>
<pre class="prettyprint lang-xml"><span style="font-family: inherit;"><?xml version="1.0" encoding="UTF-8" ?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
version="1.0">
<persistence-unit name="persistenceUnit" transaction-type="RESOURCE_LOCAL">
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect" />
<property name="hibernate.show_sql" value="true" />
</properties>
</persistence-unit>
</persistence></span></pre>
<span style="font-family: inherit;"><br /></span>
<br />
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><b>Step 6:</b> configuration files - l<span style="font-family: "courier new" , "courier" , monospace;">og4j.properties</span></span></div>
<span style="font-family: inherit;"><br /></span>
<br />
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;">As I wrote before we will use Log4j instead of Hibernate's default SLF4j. Lets's log everything we can (we can see how Hibernate opens/closes connections, how connections are taken from the pool, how Spring manages transactions and so on):</span></div>
<pre class="prettyprint lang-xml"><span style="font-family: inherit;">log4j.appender.A1=org.apache.log4j.ConsoleAppender
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%d{HH:mm:ss,SSS} %-5p [%c] - %m%n
log4j.rootLogger=info,A1
log4j.logger.com.mchange.v2=DEBUG
log4j.logger.org.hibernate=DEBUG
log4j.logger.org.springframework=DEBUG</span></pre>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><br /></span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><b>Step 7:</b> database entities.</span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><br /></span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;">As shown at the beginning of previous post, we had only one entity represented by the class <span style="font-family: "courier new" , "courier" , monospace;">Bike.java</span>. Instances of Bike.java where created inside Spring service nam<code></code>ed <span style="font-family: "courier new" , "courier" , monospace;">bikeDataProvider</span> (<span style="font-family: "courier new" , "courier" , monospace;">BikeDataProviderImp.java</span>). Now we have a database with tables Bike, Bike_Category, Account and Role. For each table we should create one class (note: this is the simplest mapping one table to one class - there are other way to map tables to classes).</span></div>
<span style="font-family: inherit;"><br /></span>
<br />
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;">Bike entity:</span></div>
<pre class="prettyprint lang-java"><span style="font-family: inherit;">package com.jsfsample.model;
@Entity
@Table(name="bike")
public class Bike implements Serializable {
private Integer id;
private BikeCategory category;
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="bike_id")
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
@ManyToOne
@JoinColumn(name="bike_category_id")
public BikeCategory getCategory() {
return category;
}
public void setCategory(BikeCategory category) {
this.category = category;
}
// rest of columns
}</span></pre>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;">Bike_category entity:</span></div>
<pre class="prettyprint lang-java"><span style="font-family: inherit;">package com.jsfsample.model;
@Entity
@Table(name="bike_category")
public class BikeCategory {
private Integer categoryId;
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="bike_category_id")
public Integer getCategoryId() {
return categoryId;
}
public void setCategoryId(Integer categoryId) {
this.categoryId = categoryId;
}
// rest of columns
}</span></pre>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;">Account entity:</span></div>
<pre class="prettyprint lang-java"><span style="font-family: inherit;">package com.jsfsample.model;
@Entity
@Table(name="account")
public class Person {
private Integer accountId;
private Role currentRole;
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="account_id")
public Integer getAccountId() {
return accountId;
}
public void setAccountId(Integer accountId) {
this.accountId = accountId;
}
@ManyToOne
@JoinColumn(name="role_id")
public Role getCurrentRole() {
return currentRole;
}
public void setCurrentRole(Role currentRole) {
this.currentRole = currentRole;
}
// rest of columns
}</span></pre>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;">Role entity:</span></div>
<pre class="prettyprint lang-java"><span style="font-family: inherit;">package com.jsfsample.model;
@Entity
@Table(name="role")
public class Role {
private Integer roleId;
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="role_id")
public Integer getRoleId() {
return roleId;
}
public void setRoleId(Integer roleId) {
this.roleId = roleId;
}
// rest of columns
}</span></pre>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;">Note how classes are mapped to tables (using <span style="font-family: "courier new" , "courier" , monospace;">@Table(name="...")</span>) and how fields are mapped to columns (using <span style="font-family: "courier new" , "courier" , monospace;">@Column(name="...")</span>). We also mapped many-to-one relations between tables using <span style="font-family: "courier new" , "courier" , monospace;">@ManyToOne</span> annotation with proper joim column name. Note also how primary keys are generated - we do not use PostgreSQL sequence (<span style="font-family: "courier new" , "courier" , monospace;">GenerationType.SEQUENCE</span>) - PostgreSQL supports <span style="font-family: "courier new" , "courier" , monospace;">SERIAL</span> data type for primary keys, which actually hides sequences (see <a href="http://wiki.postgresql.org/wiki/FAQ#How_do_I_create_a_serial.2Fauto-incrementing_field.3F">this link</a>).</span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><br /></span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><b>Step 8:</b> repositories and services.</span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><br /></span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;">Spring repositories are similar to DAO. All database interactions is done insde them. As an example, let's look inside repository resposnible for loading and saving bikes - <span style="font-family: "courier new" , "courier" , monospace;">BikesDAOImpl.java</span>:</span></div>
<span style="font-family: inherit;"><br /></span>
<br />
<pre class="prettyprint lang-java"><span style="font-family: inherit;">package com.jsfsample.repositories.impl;
@Repository("BikesRepository")
public class BikesDAOImpl implements BikesDAO {
private EntityManager em = null;
@PersistenceContext
public void setEntityManager(EntityManager em) {
this.em = em;
}
@Override
public Bike loadSelectedBike(Integer bikeId) {
return em.find(Bike.class, bikeId);
}
// other methods for saving bike and loading bike for given category
}</span></pre>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><br /></span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;">This repository is used in Spring's service named <span style="font-family: "courier new" , "courier" , monospace;">bikeDataProvider </span>(<span style="font-family: "courier new" , "courier" , monospace;">BikeDataProviderImpl.java</span>):</span></div>
<span style="font-family: inherit;"><br /></span>
<br />
<pre class="prettyprint lang-java"><span style="font-family: inherit;">package com.jsfsample.services.impl;
...
@Service("bikeDataProvider")
public class BikeDataProviderImpl implements BikeDataProvider {
@Resource(name="BikesRepository")
private BikesDAO bikesRepository;
@Resource(name="DictionaryRepository")
private DictionariesDAO dictionaryRepository;
public Bike getBikeById(Integer id){
return bikesRepository.loadSelectedBike(id);
}
@Transactional
public void add(Bike newBike, Integer categoryId) {
BikeCategory categorySelected = dictionaryRepository.loadBikeCategroryById(categoryId);
newBike.setCategory(categorySelected);
bikesRepository.saveBike(newBike);
}
// other methods using repositories
}</span></pre>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;">Similar solution is used for account (person) repository which is then used in Spring's Security service named <span style="font-family: "courier new" , "courier" , monospace;">userDetailsService</span> (<span style="font-family: "courier new" , "courier" , monospace;">UserDetailsServiceImpl.java</span>). Note how easy is to execute a method within database transaction - just use <span style="font-family: "courier new" , "courier" , monospace;">@Transactional</span> annotation and voila.</span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><br /></span></div>
<div style="font-family: Verdana,sans-serif;">
<b><span style="font-family: inherit;">That's all about adding JPA into Spring-based JSF2 sample application.</span></b></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><br /></span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;">The rest of application code (web pages, JSF managed beans) are the same as in pure Spring example project - we only modified services by providing repositories.<b> </b></span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><br /></span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;">How to test it? After deploying application on the server and starting the server, we have to open a browser and type in URL:</span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><br /></span></div>
<div style="font-family: Verdana,sans-serif;">
<b><span style="font-family: inherit;">http://localhost:8080/JSF2FeaturesSpringJPA</span></b></div>
<div style="font-family: Verdana,sans-serif;">
</div>
<div style="font-family: Verdana,sans-serif;">
</div>
<span style="font-family: inherit;"><br /></span>
<br />
<div style="font-family: inherit;">
<span style="font-family: inherit;"><br /></span></div>
<div style="color: red;">
<span style="font-family: inherit;">------------------------------------------- </span></div>
<div style="color: red;">
<span style="font-family: inherit;">Download source files:</span></div>
<div style="color: red;">
</div>
Note: make sure that <a href="http://technology-for-human.blogspot.com/2009/04/instalacja-java-jdk-prawie-bezbolesna.html">Java</a>, <a href="http://technology-for-human.blogspot.com/2009/04/instalacja-i-konfiguracja-ide-eclipse.html">Eclipse</a> and <a href="http://technology-for-human.blogspot.com/2009/05/konfiguracja-tomcata-w-eclipse.html">Tomcat</a> are properly installed and configured for running the project (additional configuration may be required if different directories are used).<br />
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><br /></span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;">Eclipse complete sample project is <a href="https://drive.google.com/open?id=1kt-TyWVnVnnklzVdEAaD-qApaup_H38X">here</a> (with all required libraries). The sample project is a ready to run application which contains JPA (Hibernate), Spring (with Spring Security). You can also download a war file located <a href="https://drive.google.com/open?id=12_HeDcgqZrpXE8WAn1s-WpVHumR8TI3N">here</a> (just copy it inside webapps folder in Your Tomcat and start Tomcat with the script startup.bat). Make sure that before doing this You created required database and PostgreSQL server is up and running (You can check access using pgAdmin III).</span></div>
Paweł Nieściorukhttp://www.blogger.com/profile/12640446237233312925noreply@blogger.com7tag:blogger.com,1999:blog-160318223199333541.post-81044741144089099842011-03-09T21:28:00.002+01:002018-12-16T13:58:02.399+01:00JSF 2, Spring 3, JPA (Hibernate 3), PostgreSQL, c3p0 - everything together (part 1 of 2)<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><b>Note: </b>below description uses <b>Eclipse Indigo</b>, <b>Tomcat 7.0.28</b>,<b> MyFaces 2.1.7</b>, <b>Spring Framework 3.1.1</b>, <b>Spring Security 3.1.0</b>,<b> Hibernate 3.6.10 (Final)</b>,<b> </b><b>PostgreSQL 9.1</b>.</span><br />
<span style="font-family: inherit;"><br /></span>
<b><span style="font-family: inherit;">Requirements:</span></b></div>
<ul style="font-family: Verdana,sans-serif;">
<li><span style="font-family: inherit;">a working example of JSF 2.0 application with Spring Framework and Spring Security integrated (can be found <a href="http://technology-for-human.blogspot.com/2011/01/jsf-2-with-spring-3-protection-with.html">here</a>)</span></li>
<li><span style="font-family: inherit;">installed PostgreSQL database (simple installation variant described <a href="http://technology-for-human.blogspot.com/2009/04/instalacja-postgresql-wersja-soft.html">here</a>)</span></li>
</ul>
<div style="font-family: Verdana,sans-serif;">
<b><span style="font-family: inherit;">You will learn:</span></b></div>
<ul style="font-family: Verdana,sans-serif;">
<li><span style="font-family: inherit;">how to use database for loading and storing the data in the sample JSF 2.0 application</span></li>
<li><span style="font-family: inherit;">how to access the database from the application using JPA (Hibernate) and Spring</span></li>
<li><span style="font-family: inherit;">how to optimize database access by using connection pool (c3p0)</span></li>
</ul>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;">It is a high time to create a real working example with a database. Sample application used in previous posts use no database. We had Spring's managed service named <span style="font-family: "Courier New",Courier,monospace;">bikeDataProvider</span> acting as a database - all bikes data were created in that service during startup, all bikes data were retrieved from that service, even new bike data were stored internally in the service:</span></div>
<pre class="prettyprint" style="font-family: Verdana,sans-serif;"><span style="font-family: inherit;">package com.jsfsample.services.impl;
...
@Service("bikeDataProvider")
public class BikeDataProviderImpl implements BikeDataProvider {
private List<Bike> bikes;
@PostConstruct
private void prepareData(){
bikes = new ArrayList<Bike>();
// MTB
Bike mtb1 = new Bike();
mtb1.setId(1);
mtb1.setName("Kellys Mobster");
mtb1.setDescription("Kellys Mobster, lorem ipsut...");
mtb1.setPrice(6500);
mtb1.setCategory(1);
// ... rest of mock bikes created here
}
public List<Bike> getBikesByCategory(Integer categoryId, boolean onlyWithDiscount) {
// returns bikes by given category
}
public Bike getBikeById(Integer id){
// returns certain bike for its details
}
@Override
public void add(Bike newBike) {
// add new bike
bikes.add(newBike);
}
}</span></pre>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;">The same method of creating mock data was used in Spring's managed service named <span style="font-family: "Courier New",Courier,monospace;">userDetailsService</span> - users and their authorities were created directly inside the service:</span></div>
<pre class="prettyprint" style="font-family: Verdana,sans-serif;"><span style="font-family: inherit;">package com.jsfsample.application.impl;
...
@Service("userDetailsService")
public class UserDetailsServiceImpl implements UserDetailsService {
private HashMap<String, org.springframework.security.core.userdetails.User> users = new HashMap<String, org.springframework.security.core.userdetails.User>();
@Override
public UserDetails loadUserByUsername(String username) {
// returns user
}
@PostConstruct
public void init() {
// mocked roles
Collection<GrantedAuthority> adminAuthorities = new ArrayList<GrantedAuthority>();
adminAuthorities.add(new GrantedAuthorityImpl("ROLE_ADMIN"));
Collection<GrantedAuthority> userAuthorities = new ArrayList<GrantedAuthority>();
userAuthorities.add(new GrantedAuthorityImpl("ROLE_REGISTERED"));
boolean enabled = true;
boolean accountNonExpired = true;
boolean credentialsNonExpired = true;
boolean accountNonLocked = true;
// mocked users with roles
users.put("admin", new org.springframework.security.core.userdetails.User("admin", "admin", enabled, accountNonExpired,
credentialsNonExpired, accountNonLocked, adminAuthorities));
users.put("user", new org.springframework.security.core.userdetails.User("user", "user", enabled, accountNonExpired,
credentialsNonExpired, accountNonLocked, userAuthorities));
}
}</span></pre>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;">In both cases all data are created in the method annotated with <span style="font-family: "Courier New",Courier,monospace;">@PostConstruct</span>. That annotation means, that method will be invoked first after creating an instance of a class (in this case Spring creates instances of those service classes).</span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><br /></span></div>
<div style="font-family: Verdana,sans-serif;">
<b><span style="font-family: inherit;">Our objective is to remove that mocked data created explicite in the services classes and replace them with data coming from real database.</span></b></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><br /></span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><b>Step 1:</b> we need database structure:</span></div>
<pre class="prettyprint lang-sql" style="font-family: Verdana,sans-serif;"><span style="font-family: inherit;">CREATE TABLE "role" (
role_id SERIAL NOT NULL,
name VARCHAR(32),
CONSTRAINT role_pk PRIMARY KEY (role_id)
);
CREATE TABLE "account" (
account_id SERIAL NOT NULL,
role_id INTEGER NOT NULL,
login VARCHAR(32),
password VARCHAR(32),
CONSTRAINT account_pk PRIMARY KEY (account_id),
CONSTRAINT role_id_fk FOREIGN KEY (role_id) REFERENCES role(role_id)
);
CREATE TABLE "bike_category" (
bike_category_id SERIAL NOT NULL,
name VARCHAR(32),
CONSTRAINT bike_category_pk PRIMARY KEY (bike_category_id)
);
CREATE TABLE "bike" (
bike_id SERIAL NOT NULL,
bike_category_id INTEGER NOT NULL,
name VARCHAR(32),
description TEXT,
price numeric(10,2),
discount_price numeric(10,2),
CONSTRAINT bike_pk PRIMARY KEY (bike_id),
CONSTRAINT bike_category_fk FOREIGN KEY (bike_category_id) REFERENCES bike_category(bike_category_id)
);
</span></pre>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;">This is very simple database with two many-to-one relations: many users belongs to (have) one role and many bikes belongs to (have) one category. Of course in real world user would have many roles, so we would use many-to-many relation, but I decided to use many-to-one to simplify it.</span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;">Tables account and role will be used by Spring Security - they store users and their authorities (roles). Tables bike and bike_category are the "heart" of bike store.</span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><br /></span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><b>Step 2:</b> when structure is ready, it is time to insert some sample data:</span></div>
<pre class="prettyprint lang-sql" style="font-family: Verdana,sans-serif;"><span style="font-family: inherit;">INSERT INTO role (name) values ('ROLE_ADMIN'); -- id 1
INSERT INTO role (name) values ('ROLE_REGISTERED'); -- id 2
INSERT INTO account (role_id, login, password) values (1, 'admin', 'admin'); -- ROLE_ADMIN
INSERT INTO account (role_id, login, password) values (2, 'user', 'user'); -- ROLE_REGISTERED
INSERT INTO bike_category (name) values ('Mountain'); -- id 1
INSERT INTO bike_category (name) values ('Trekking'); -- id 2
INSERT INTO bike_category (name) values ('Cross'); -- id 3
INSERT INTO bike (bike_category_id, name, description, price, discount_price) values (1, 'Kellys Mobster', 'Kellys Mobster, lorem ipsut...', 6500, null); -- Mountain
INSERT INTO bike (bike_category_id, name, description, price, discount_price) values (1, 'Scott Scale', 'Scott Scale, lorem ipsut...', 18900, null); -- Mountain
INSERT INTO bike (bike_category_id, name, description, price, discount_price) values (1, 'Author Magnum', 'Author Magnum, lorem ipsut...', 17200, 15500); -- Mountain
INSERT INTO bike (bike_category_id, name, description, price, discount_price) values (2, 'Giant Accend', 'Giant Accend, lorem ipsut...', 5000, 4600); -- Trekking
INSERT INTO bike (bike_category_id, name, description, price, discount_price) values (2, 'Merida Freeway', 'Merida Freeway, lorem ipsut...', 2400, 2100); -- Trekking
INSERT INTO bike (bike_category_id, name, description, price, discount_price) values (2, 'Mbike Massive', 'Mbike Massive, lorem ipsut...', 1900, null); -- Trekking
INSERT INTO bike (bike_category_id, name, description, price, discount_price) values (3, 'Giant Roam XR 1', 'Giant Roam XR 1, lorem ipsut...', 3900, null); -- Cross
INSERT INTO bike (bike_category_id, name, description, price, discount_price) values (3, 'Cannondale Quick Cx', 'Cannondale Quick Cx, lorem ipsut...', 4999, null); -- Cross
INSERT INTO bike (bike_category_id, name, description, price, discount_price) values (3, 'Cube Cross', 'Cube Cross, lorem ipsut...', 4500, 4200); -- Cross
</span></pre>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><br /></span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><b>Step 3:</b> adding required libraries into project.</span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><br /></span>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEio6DP_TLrZpfkm4aNEFJcZrNsCpQsxQOQXuP0pdjg_Ye3u0HX1l34qJvmXhCnCrYTf9DeSQl9h7ck9cbeUQwYcT60mLp1EqThuFHKvYbiXnY3nwxSaJd1SNhOZ3GgSgxB73u2L6oK8Nv_L/s1600/2011.03.09_1.PNG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><span style="font-family: inherit;"><img border="0" height="316" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEio6DP_TLrZpfkm4aNEFJcZrNsCpQsxQOQXuP0pdjg_Ye3u0HX1l34qJvmXhCnCrYTf9DeSQl9h7ck9cbeUQwYcT60mLp1EqThuFHKvYbiXnY3nwxSaJd1SNhOZ3GgSgxB73u2L6oK8Nv_L/s400/2011.03.09_1.PNG" width="400" /></span></a></div>
</div>
<div class="separator" style="clear: both; text-align: center;">
</div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><br /></span>
<span style="font-family: inherit;">Comparing those libraries with libraries from clean JSF 2.0 project (look <a href="http://1.bp.blogspot.com/_JXxdCCKdTo0/TJEDMF3peKI/AAAAAAAAFRs/Icr94q8io-g/s1600/JSF2_Eclipse_project.PNG">here</a>) or libraries from JSF 2.0 project with Spring (look <a href="http://3.bp.blogspot.com/_JXxdCCKdTo0/TQfWXfhx6SI/AAAAAAAAFlU/bgXDKk1aFAc/s1600/JSF2_Features_Spring_1.PNG">here</a>) we have extra libraries here: Hibernate libraries marked green, Spring JPA libraries marked red and some third party libraries marked blue. Those blue libraries are:</span><br />
<ul>
<li><span style="font-family: inherit;">c3p0 libraries for connection pool</span></li>
<li><span style="font-family: inherit;">log4j library and bridge library from sl4j to log4j - Hibernate by default uses sl4j, but we would like to use log4j</span></li>
<li><span style="font-family: inherit;">JDBC driver for PostgreSQL</span></li>
</ul>
</div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><b>Step 4:</b> configuration files - <i><span style="font-family: Verdana,sans-serif;">will be continued in next post.</span></i></span><br />
<span style="font-family: inherit;"><br /></span>
<div style="color: red;">
<span style="font-family: inherit;">------------------------------------------- </span></div>
<div style="color: red;">
<span style="font-family: inherit;">Download source files:</span></div>
<span style="font-family: inherit;">The complete working example of mentioned application which will contain all described issues, will be available in the last (second) article of this serie.</span></div>
Paweł Nieściorukhttp://www.blogger.com/profile/12640446237233312925noreply@blogger.comtag:blogger.com,1999:blog-160318223199333541.post-31882962099735179712011-01-28T21:37:00.006+01:002018-12-16T15:19:04.907+01:00JSF 2 with Spring 3 - protection with Spring Security (part 2 of 2)<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><b>Note: </b>below description uses <b>Eclipse Indigo</b>, <b>Tomcat 7.0.28</b>,<b> MyFaces 2.1.7</b>, <b>Spring Framework 3.1</b>,<b> Spring Security 3.1.0</b>.</span><br />
<b><span style="font-family: inherit;"> </span></b><br />
<span style="font-family: inherit;"><br /></span>
<b><span style="font-family: inherit;">Requirements:</span></b></div>
<ul style="font-family: Verdana,sans-serif;">
<li><span style="font-family: inherit;">a working example of JSF 2.0 application with Spring Framework integrated (can be found <a href="http://technology-for-human.blogspot.com/2010/12/jsf-2-with-spring-3-basics-part-1-of-2.html">here</a>)</span></li>
</ul>
<div style="font-family: Verdana,sans-serif;">
<b><span style="font-family: inherit;">You will learn:</span></b></div>
<ul style="font-family: Verdana,sans-serif;">
<li><span style="font-family: inherit;">how to use Spring Security Framework in order to protect web application</span></li>
</ul>
<span style="font-family: inherit;">From the previous <a href="http://technology-for-human.blogspot.com/2010/12/jsf-2-with-spring-3-basics-part-1-of-2.html">post</a> we know what Spring Framework is, and what advantages it gives us when used in web application. Business logic managed by Spring is not the only one advantage coming from Spring - we can use <b>Spring's embedded mechanisms to secure our web application</b>. This post will show the basic usage of Spring Security for securing our sample JSF 2.0 webapp. </span><br />
<span style="font-family: inherit;">What parts of our application will be protected? Consider those scenarios: </span><br />
<span style="font-family: inherit;"><b>1.</b> Only registered user (or page administrator) can see details of a selected bike.</span><br />
<span style="font-family: inherit;"><b>2.</b> Only page administrator can add a new bike to the shop offer.</span><br />
<span style="font-family: inherit;"><br /></span>
<span style="font-family: inherit;">We need two user roles which will determine the privilleges which user has: <i>registered users</i> role and <i>admin users</i> role. Moreover, for the scenario 2, we have to add a new function: adding new bike. For this function we will create a page <span style="font-family: "courier new" , "courier" , monospace;">addBike.xhtml</span> and a JSF managed bean for that page, named <span style="font-family: "courier new" , "courier" , monospace;">addBike.java</span>. </span><br />
<span style="font-family: inherit;"><br /></span>
<span style="font-family: inherit;"><span style="font-family: "courier new" , "courier" , monospace;">addBike.xhtml</span> source code is shown below:</span><br />
<pre class="prettyprint"><span style="font-family: inherit;"><?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core">
<ui:composition template="../shopTemplate.xhtml">
<ui:define name="content">
<h:form>
<h:outputText value="#{msg['bikes.list.name']}: "/><h:inputText value="#{addBike.name}" /><br/>
<h:outputText value="#{msg['bikes.list.price']}: "/><h:inputText value="#{addBike.price}" /><br/>
<h:outputText value="#{msg['bikes.list.discountprice']}: "/><h:inputText value="#{addBike.discountPrice}" /><br/>
<h:outputText value="#{msg['bikes.list.description']}: "/><h:inputText value="#{addBike.description}" /><br/>
<h:commandButton action="#{addBike.addNewBike}" value="#{msg['bikes.add.button']}" />
</h:form>
</ui:define>
</ui:composition>
</html></span></pre>
<span style="font-family: inherit;">Nothing special - standard form for entering the data.</span><br />
<span style="font-family: inherit;"><span style="font-family: "courier new" , "courier" , monospace;">addBike.java</span> source code is also simple:</span><br />
<pre class="prettyprint"><span style="font-family: inherit;">package com.jsfsample.managedbeans;
...
@ManagedBean(name="addBike")
@SessionScoped
public class AddBike implements Serializable {
private static final long serialVersionUID = -2155913853431899821L;
@ManagedProperty("#{bikeDataProvider}")
private BikeDataProvider bikeDataProvider; // injected Spring defined service for bikes
private String name;
private String description;
private String price;
private String discountPrice;
private Integer categoryId;
public String addNewBike(){
Bike newBike = new Bike();
newBike.setName(getName());
newBike.setDescription(getDescription());
newBike.setPrice(Integer.parseInt(getPrice()));
newBike.setDiscountPrice(Integer.parseInt(getDiscountPrice()));
newBike.setCategory(categoryId);
// save new bike and return to the shop
bikeDataProvider.add(newBike);
return "/bikesShop.xhtml";
};
...
}
</span></pre>
<span style="font-family: inherit;">Please note that we use here <span style="font-family: "courier new" , "courier" , monospace;">BikeDataProvider.java</span> class, which is Spring managed service, the same we used for loading bikes list and loading a certain bike details in previous <a href="http://technology-for-human.blogspot.com/2010/12/jsf-2-with-spring-3-basics-part-1-of-2.html">post</a>. </span><br />
<span style="font-family: inherit;"><br /></span>
<span style="font-family: inherit;">Now it is time for protected parts of application. I will show two ways of protecting webapp: <b>protecting resources</b> (like access to certain page) and <b>protecting business logic methods execution</b>. <b>Scenario 1 will be an example of protecting business logic</b> and<b> scenario 2 will be an example of protecting resources</b>.</span><br />
<span style="font-family: inherit;">When user tries to access the protected area (resource or invoke protected method), application will check user roles and based on them will decide if let the user go further or force him to log in. Log in - that's right - a login page will be displayed where user will enter his credentials. Based on them Spring Security will decide what roles user has and depends on assigned roles further action will be continued or not. Let's modify our application to use Spring Security:</span><br />
<span style="font-family: inherit;"><br /></span>
<span style="font-family: inherit;"><b>Step 1. </b>Modify configuration files:</span><br />
<span style="font-family: inherit;"><span style="font-family: "courier new" , "courier" , monospace;">applicationContext.xml</span> source:</span><br />
<pre class="prettyprint lang-xml"><span style="font-family: inherit;">...
<!--
resource security
-->
<sec:http auto-config="true" access-denied-page="/faces/accessDenied.xhtml">
<sec:form-login login-page="/faces/login.xhtml" />
<sec:intercept-url pattern="/faces/admin/**" access="ROLE_ADMIN" />
</sec:http>
<!--
business logic (method) security
-->
<sec:global-method-security
secured-annotations="enabled" jsr250-annotations="enabled" >
</sec:global-method-security>
<!--
manager responsible for loading user account with assigned roles
-->
<sec:authentication-manager alias="authenticationManager">
<sec:authentication-provider
user-service-ref="userDetailsService" />
</sec:authentication-manager>
...
</span></pre>
<span style="font-family: inherit;">Access-denied-page is invoked when user is <b>authenticated</b> but is not <b>authorized</b> to access protected resources. When user is <b>not authenticated</b>, he is moved into form-login instead of access-denied-page.</span><br />
<span style="font-family: inherit;"> <span style="font-family: "courier new" , "courier" , monospace;">web.xml</span> source:</span><br />
<pre class="prettyprint lang-xml"><span style="font-family: inherit;">...
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
...</span></pre>
<span style="font-family: inherit;"><b>Step 2.</b> Additional pages: <span style="font-family: "courier new" , "courier" , monospace;">login.xhtml</span> and <span style="font-family: "courier new" , "courier" , monospace;">accessDenied.xhtml</span>.</span><br />
<span style="font-family: inherit;"><span style="font-family: "courier new" , "courier" , monospace;">accessDenied.xhtml</span> is simple page displaying only a message saying that the user is authenticated but still is not authorized to go further.</span><br />
<span style="font-family: inherit;"><span style="font-family: "courier new" , "courier" , monospace;">login.xhtml</span> is a simple page with login form where user enters his credentials (login and password). The more interesting part is corresponding managed bean LoginBean.java which uses a Spring service for authenticating users:</span><br />
<pre class="prettyprint"><span style="font-family: inherit;">package com.jsfsample.managedbeans;
...
@ManagedBean(name = "loginBean")
@SessionScoped
public class LoginBean implements Serializable {
private static final long serialVersionUID = 1L;
private String login;
private String password;
@ManagedProperty(value = "#{authenticationService}")
private AuthenticationService authenticationService; // injected Spring defined service for bikes
public String login() {
boolean success = authenticationService.login(login, password);
if (success){
return "bikesShop.xhtml"; // return to application but being logged now
}
else{
FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Login or password incorrect."));
return "login.xhtml";
}
}
...
}</span></pre>
<span style="font-family: inherit;">When login was successful and user is authenticated, he is moved to the shop. If not a proper message is displayed and user can re-enter his credentials or go back to the shop without login. Let's look inside AuthenticationService.java class which is a service deciding if user is authenticated or not.</span><br />
<span style="font-family: inherit;"> </span><br />
<span style="font-family: inherit;"><b>Step 3.</b> AuthenticationService implementation:</span><br />
<pre class="prettyprint"><span style="font-family: inherit;">package com.jsfsample.application.impl;
...
@Service("authenticationService")
public class AuthenticationServiceImpl implements com.jsfsample.application.AuthenticationService {
@Resource(name = "authenticationManager")
private AuthenticationManager authenticationManager; // specific for Spring Security
@Override
public boolean login(String username, String password) {
try {
Authentication authenticate = authenticationManager
.authenticate(new UsernamePasswordAuthenticationToken(
username, password));
if (authenticate.isAuthenticated()) {
SecurityContextHolder.getContext().setAuthentication(
authenticate);
return true;
}
} catch (AuthenticationException e) {
}
return false;
}
...
}</span></pre>
<span style="font-family: inherit;">This Spring managed service uses internally a class <span style="font-family: "courier new" , "courier" , monospace;">AuthenticationManager</span>, which comes from Spring Security and was defined as a manager in <span style="font-family: "courier new" , "courier" , monospace;">applicationContext.xml</span> file:</span><br />
<pre class="prettyprint lang-xml"><span style="font-family: inherit;">...
<!--
manager responsible for loading user account with assigned roles
-->
<sec:authentication-manager alias="authenticationManager">
<sec:authentication-provider
user-service-ref="userDetailsService" />
</sec:authentication-manager>
...
</span></pre>
<span style="font-family: inherit;">Note that we do not explicit define <span style="font-family: "courier new" , "courier" , monospace;">AuthenticationManager</span>! It is a ready to use object. But <span style="font-family: "courier new" , "courier" , monospace;">AuthenticationManager</span> has helper service named <span style="font-family: "courier new" , "courier" , monospace;">userDetailService</span> defined in applicationContext.xml file - this service must be written by our own.<br />
</span><br />
<span style="font-family: inherit;"><span style="font-family: "verdana" , sans-serif;"><b>Step 4.</b> Implementation of </span><span style="font-family: "verdana" , sans-serif;"><span style="font-family: "courier new" , "courier" , monospace;">userDetailService</span></span><span style="font-family: "verdana" , sans-serif;">. </span></span><br />
<span style="font-family: inherit;"><span style="font-family: "courier new" , "courier" , monospace;">userDetailsService</span> source code is shown below:</span><br />
<pre class="prettyprint"><span style="font-family: inherit;">package com.jsfsample.application.impl;
...
@Service("userDetailsService")
public class UserDetailsServiceImpl implements UserDetailsService {
private HashMap<string org.springframework.security.core.userdetails.user=""> users = new HashMap<string org.springframework.security.core.userdetails.user="">();
@Override
public UserDetails loadUserByUsername(String username)
throws UsernameNotFoundException{
org.springframework.security.core.userdetails.User user = users.get(username);
if (user == null) {
throw new UsernameNotFoundException("UserAccount for name \""
+ username + "\" not found.");
}
return user;
}
@PostConstruct
public void init() {
// sample roles
Collection<grantedauthority> adminAuthorities = new ArrayList<grantedauthority>();
adminAuthorities.add(new GrantedAuthorityImpl("ROLE_ADMIN"));
Collection<grantedauthority> userAuthorities = new ArrayList<grantedauthority>();
userAuthorities.add(new GrantedAuthorityImpl("ROLE_REGISTERED"));
boolean enabled = true;
boolean accountNonExpired = true;
boolean credentialsNonExpired = true;
boolean accountNonLocked = true;
// sample users with roles set
users.put("admin", new org.springframework.security.core.userdetails.User("admin", "admin", enabled, accountNonExpired,
credentialsNonExpired, accountNonLocked, adminAuthorities));
users.put("user", new org.springframework.security.core.userdetails.User("user", "user", enabled, accountNonExpired,
credentialsNonExpired, accountNonLocked, userAuthorities));
}
}</grantedauthority></grantedauthority></grantedauthority></grantedauthority></string></string></span></pre>
<span style="font-family: inherit;">We have here a Spring Security specific objects representing users and roles. In the <span style="font-family: "courier new" , "courier" , monospace;">init()</span> method I created some mocked data two roles representing page administrators and registered users - ROLE_ADMIN and ROLE_REGISTERED. For each role I created a single account: admin (password: admin) for the ROLE_ADMIN and user (password: user) for the ROLE_REGISTERED. That's all - it is time to protect applicartion.</span><br />
<span style="font-family: inherit;"><br /></span>
<span style="font-family: inherit;"><b>Step 5.</b> Protecting application.</span><br />
<span style="font-family: inherit;"><b>5 a)</b> Scenario 1: protecting business logic. We have to protect invoking a method which allows to see bike details. This method is placed inside <span style="font-family: "courier new" , "courier" , monospace;">BikeDataProvider.java</span> service class. In order to protect the method we have to add an annotation defining roles allowed to execute this method:</span><br />
<pre class="prettyprint"><span style="font-family: inherit;">package com.jsfsample.services;
...
public interface BikeDataProvider {
...
@RolesAllowed({"ROLE_ADMIN","ROLE_REGISTERED"})
public abstract Bike getBikeById(Integer id);
public abstract void add(Bike newBike);
}</span></pre>
<span style="font-family: inherit;">This simply means that <b>only registered users or admin users</b> can see bike details. </span><br />
<span style="font-family: inherit;">Why we do not protect the <span style="font-family: "courier new" , "courier" , monospace;">add(...)</span> method? Because we protect the whole page access where this method is executed - of course in addition we can also protect this method by annotating it with <span style="font-family: "courier new" , "courier" , monospace;">@RolesAllowed({"ROLE_ADMIN"})</span>.</span><br />
<span style="font-family: inherit;"><b>5 b)</b> Scenario 2: protecting resource. According to rules of protecting resources defined in <span style="font-family: "courier new" , "courier" , monospace;">applicationContext.xml</span>, we protect all resources which are located inside <span style="font-family: "courier new" , "courier" , monospace;">/admin</span> directory. So we have to create a directory <span style="font-family: "courier new" , "courier" , monospace;">/admin</span> under <span style="font-family: "courier new" , "courier" , monospace;">/WebContent</span> directory and move <span style="font-family: "courier new" , "courier" , monospace;">addBike.xhtml</span> page there. It should look like this:</span><br />
<span style="font-family: inherit;"><br /></span>
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiIOJZEIUTz1r-hxcZMx8x2ubUEcxHrYNis8MVaWh54OgjAb2Rl1c3m-pc7rMECfRzIMGDuWwaRTTPLzyUsg3810MTly57sOxZnQedxoBYk19N5ZgARfsC8c4RJTKU7D3cud-mkZtNT-VTv/s1600/2011.01.28_1.PNG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><span style="font-family: inherit;"><img border="0" height="366" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiIOJZEIUTz1r-hxcZMx8x2ubUEcxHrYNis8MVaWh54OgjAb2Rl1c3m-pc7rMECfRzIMGDuWwaRTTPLzyUsg3810MTly57sOxZnQedxoBYk19N5ZgARfsC8c4RJTKU7D3cud-mkZtNT-VTv/s400/2011.01.28_1.PNG" width="400" /></span></a></div>
<span style="font-family: inherit;"> </span><br />
<div class="separator" style="clear: both; text-align: center;">
</div>
<span style="font-family: inherit;"><br /></span>
<span style="font-family: inherit;"><b><span style="color: red;">Note:</span></b> there is a little trick in the protecting resources like pages in JSF. Spring Security tries to match exact URL address to apply the rule. But in JSF there is a "old URL" issue - after navigtation from page A to page B, URL address in browser still points to page A. In order to make the rule working we have to force the browser to show the current URL instead of old one. It is done by adding a special command into the navigation string returning a page for adding a bike:</span><br />
<pre class="prettyprint"><span style="font-family: inherit;">public String showForm(){
...
return "/admin/addBike.xhtml?faces-redirect=true";
}</span></pre>
<span style="font-family: inherit;"><br /></span>
<br />
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><b>That's all about Spring Security in our sample application.</b> </span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;">How to test it? After deploying application on the server and starting the server, we have to open a browser and type in URL:</span><br />
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><br /></span></div>
<div style="font-family: Verdana,sans-serif;">
<b><span style="font-family: inherit;">http://localhost:8080/JSF2FeaturesSpring</span></b></div>
<div style="font-family: Verdana,sans-serif;">
</div>
<span style="font-family: inherit;"><br /></span>
<span style="font-family: inherit;">Then try to display some bike details. When promped for login, enter credentials: user, user and try again. Then try to add a new bike - You should see access denied page. The close the application and clean the browser cache and try the same with the user admin, admin.</span></div>
<div style="color: red;">
<span style="font-family: inherit;"><br /></span>
<span style="font-family: inherit;">------------------------------------------- </span></div>
<div style="color: red;">
<span style="font-family: inherit;">Download source files:</span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><br /></span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><b>Note:</b> make sure that <a href="http://technology-for-human.blogspot.com/2009/04/instalacja-java-jdk-prawie-bezbolesna.html">Java</a>, <a href="http://technology-for-human.blogspot.com/2009/04/instalacja-i-konfiguracja-ide-eclipse.html">Eclipse</a> and <a href="http://technology-for-human.blogspot.com/2009/05/konfiguracja-tomcata-w-eclipse.html">Tomcat</a> are properly installed and configured for running the project (additional configuration may be required if different directories are used).</span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><br /></span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;">Eclipse complete sample project is <a href="https://drive.google.com/open?id=1aVF5RBTgw0GUN74oX514rGjJH8gqhX_-">here</a> (with all required libraries). The sample project is a ready to run application which contains all described Spring Security issues in this post. You can also download a war file located <a href="https://drive.google.com/open?id=1zNj8X3kvdyyrrIDAFlYezjm1dqYN_8Y_">here</a> (just copy it inside <span style="font-family: "courier new" , "courier" , monospace;">webapps</span> folder in Your Tomcat and start Tomcat with the script <span style="font-family: "courier new" , "courier" , monospace;">startup.bat</span>)</span></div>
Paweł Nieściorukhttp://www.blogger.com/profile/12640446237233312925noreply@blogger.com14tag:blogger.com,1999:blog-160318223199333541.post-52468026408178873162010-12-14T22:45:00.003+01:002018-12-16T13:48:05.724+01:00JSF 2 with Spring 3 - basics (part 1 of 2)<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><b>Note: </b>below description uses <b>Eclipse Indigo</b>, <b>Tomcat 7.0.28</b>,<b> MyFaces 2.1.7</b>, <b>Spring Framework 3.1.1</b>,<b> Spring Security 3.1.0</b>.</span><br />
<span style="font-family: inherit;"><br /></span>
<b><span style="font-family: inherit;">Requirements:</span></b></div>
<ul style="font-family: Verdana,sans-serif;">
<li><span style="font-family: inherit;">a working example from the <a href="http://technology-for-human.blogspot.com/2010/10/jsf-20-new-features-part-3-of-3.html">last article</a> of a serie introducing new JSF 2.0 features.</span></li>
</ul>
<div style="font-family: Verdana,sans-serif;">
<b><span style="font-family: inherit;">You will learn:</span></b></div>
<ul style="font-family: Verdana,sans-serif;">
<li><span style="font-family: inherit;">how to integrate Spring Framework into JSF 2.0 application</span></li>
</ul>
<span style="font-family: inherit;">In the previous three posts I described some interesting JSF 2.0 features and I put them together in the sample application. Those parts were mostly focused on web content, a GUI and its behaviour.</span><br />
<span style="font-family: inherit;"><span style="font-family: Verdana,sans-serif;">What about the server side? What about the business logic executed underneath? Do we had a business logic in the sample application mentioned above? Of course we had. Presenting bikes list or a certain bike under some condition - this is business logic resposniblity. Filtering bikes and presenting only those with the discount - this is also business logic. Don't think of it as a naive filtering of presented data - the business logic decides what does it mean that the certain bike has discount - it can be lower price but it can be also more complicated. </span><span style="font-family: Verdana,sans-serif;"><span style="font-family: Verdana,sans-serif;">In the previous example we had a class named <span style="font-family: "Courier New",Courier,monospace;">BikeDataProvider.java</span> which represented business logic. </span></span><span style="font-family: Verdana,sans-serif;"><span style="font-family: Verdana,sans-serif;">It was a singleton invoked with the help of static method <span style="font-family: "Courier New",Courier,monospace;">getInstance()</span> anywhere where needed.</span></span></span><br />
<span style="font-family: inherit;">When the application starts to grow up, we have more business logic accomplishing some business cases. We need something that will help us to manage the whole business logic in an elegant way - this is where Spring comes to play.</span><br />
<span style="font-family: inherit;"><span style="font-family: Verdana,sans-serif;">We will change the BikeDataProvider class into object created and managed by Spring. This object will be called service. A service which serves business logic. Then the service will be used by JSF managed beans. Let's add Spring to our sample web apllication.</span><b><span style="font-family: Verdana,sans-serif;"> </span></b></span><br />
<span style="font-family: inherit;"><br /></span>
<span style="font-family: inherit;"><b>Step 1:</b> adding required Spring libraries into the project.<b> </b></span><br />
<span style="font-family: inherit;"><br /></span>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEinC-yv4N_n1NTMxVgEUnQl5XO2n8iV0o3170NQgsfU6bkuh1O-QFh9f1oZHBM5_BOObOTFOC-Oo3PWveBBbsdMsCPCm351BCxKyWIKXbQ_jndABHOHm1VTgN-SiphJUveMb0PAB9kNIpVQ/s1600/2010.12.14_1.PNG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><span style="font-family: inherit;"><img border="0" height="281" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEinC-yv4N_n1NTMxVgEUnQl5XO2n8iV0o3170NQgsfU6bkuh1O-QFh9f1oZHBM5_BOObOTFOC-Oo3PWveBBbsdMsCPCm351BCxKyWIKXbQ_jndABHOHm1VTgN-SiphJUveMb0PAB9kNIpVQ/s400/2010.12.14_1.PNG" width="400" /></span></a></div>
<span style="font-family: inherit;"><br /></span>
<span style="font-family: inherit;"><b>Step 2:</b> creating Spring's configuration file named <span style="font-family: "Courier New",Courier,monospace;">applicationContext.xml</span>. The file has to be located inside /WEB-INF directory. This is full content of that file:</span><br />
<pre class="prettyprint"><span style="font-family: inherit;"><?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:faces="http://www.springframework.org/schema/faces"
xmlns:int-security="http://www.springframework.org/schema/integration/security"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:sec="http://www.springframework.org/schema/security"
xsi:schemaLocation="http://www.springframework.org/schema/integration/security http://www.springframework.org/schema/integration/security/spring-integration-security-3.1.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.1.xsd
http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/faces http://www.springframework.org/schema/faces/spring-faces-3.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="com.jsfsample" />
</beans>
</span></pre>
<span style="font-family: inherit;"><span style="font-family: Verdana,sans-serif;"><b></b></span><br /></span>
<span style="font-family: inherit;"><b>Step 3:</b> modifying <span style="font-family: "Courier New",Courier,monospace;">web.xml</span> by registering listener resposnible for loading Spring in web application.</span><br />
<pre class="prettyprint"><span style="font-family: inherit;"><listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
</span></pre>
<span style="font-family: inherit;"><b>Step 4:</b> modifying <span style="font-family: "Courier New",Courier,monospace;">faces-config.xml</span> file for allow JSF components use Spring components.</span><br />
<pre class="prettyprint"><span style="font-family: inherit;"><application>
... <el-resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver</el-resolver>
...
</application>
</span></pre>
<span style="font-family: inherit;"><span style="font-family: Verdana,sans-serif;"><b>Step 5:</b> modifying </span><span style="font-family: Verdana,sans-serif;"><span style="font-family: Verdana,sans-serif;"><span style="font-family: "Courier New",Courier,monospace;">BikeDataProvider.java</span> toward Spring managed service.</span></span></span><br />
<span style="font-family: Verdana,sans-serif;"><span style="font-family: inherit;"> </span></span><br />
<span style="font-family: inherit;">First of all we will add a new functionality to the application - possibility to add a new bike to the selected category. We need to create an .xhtml page with the form, then managed bean for that page and at the end a business logic method in class <span style="font-family: "Courier New",Courier,monospace;">BikeDataProvider.java</span> responsible for adding new bike to bikes' list. That's easy part - it will be visible in the attached complete example. </span><br />
<span style="font-family: inherit;">Assuming that we have this new funcitonality, we can modify .java files for Spring integration. First we create an interface: </span><br />
<pre class="prettyprint"><span style="font-family: inherit;">package com.jsfsample.services;
//imports
public interface BikeDataProvider {
public List<Bike> getBikesByCategory(Integer categoryId,
boolean onlyWithDiscount);
public Bike getBikeById(Integer id);
public void add(Bike newBike); // new function
} </span></pre>
<span style="font-family: inherit;"><span style="font-family: Verdana,sans-serif;">Then we have to create an implementation named </span><span style="font-family: Verdana,sans-serif;"><span style="font-family: "Courier New",Courier,monospace;">BikeDataProviderImpl.java</span> (yes, I know that this naming convention is bad)</span><span style="font-family: Verdana,sans-serif;"> which will have the source code from previous <span style="font-family: "Courier New",Courier,monospace;">BikeDataProvider.java</span> class. This implementation will have Spring specific annotation defining the service:</span></span><br />
<pre class="prettyprint"><span style="font-family: inherit;">package com.jsfsample.services.impl;
//imports
@Service("bikeDataProvider")
public class BikeDataProviderImpl implements BikeDataProvider {
private List<Bike> bikes;
private Integer currentBikeId;
@PostConstruct
private void prepareData(){
bikes = new ArrayList<Bike>();
// MTB
Bike mtb1 = new Bike();
mtb1.setId(1);
mtb1.setName("Kellys Mobster");
mtb1.setDescription("Kellys Mobster, lorem ipsut...");
mtb1.setPrice(6500);
mtb1.setCategory(1);
bikes.add(mtb1);
// other bikes are mocked up the same way
}
public List<Bike> getBikesByCategory(Integer categoryId, boolean onlyWithDiscount) {
// implementation
}
public Bike getBikeById(Integer id){
// implementation
}
public void add(Bike newBike) {
bikes.add(newBike);
}
} </span></pre>
<span style="font-family: inherit;"><span style="font-family: Verdana,sans-serif;">The annotation <span style="font-family: "Courier New",Courier,monospace;">@Service("bikeDataProvider")</span> means that this is Spring managed object (created by Spring) and is visible in the Spring context under the name </span><span style="font-family: Verdana,sans-serif;">"bikeDataProvider"</span><span style="font-family: Verdana,sans-serif;">. <span style="font-family: "Courier New",Courier,monospace;">@PostConstruct</span> is a little trick here - when object of this class is instantiated by Spring, the method is invoked right after the object is created. I used this for preparing demo data of bikes. The whole </span><span style="font-family: Verdana,sans-serif;"><span style="font-family: "Courier New",Courier,monospace;">BikeDataProviderImpl.java</span> </span><span style="font-family: Verdana,sans-serif;">class acts as a simple data source for the application - in the future we will use a real database instead.</span></span><br />
<span style="font-family: inherit;"><br /></span>
<span style="font-family: inherit;"><span style="font-family: Verdana,sans-serif;"><span style="font-family: Verdana,sans-serif;"><b>Step 6:</b> modifying JSF managed beans to use Spring service inside.</span></span><span style="font-family: Verdana,sans-serif;"> </span></span><br />
<span style="font-family: inherit;"><br /></span>
<span style="font-family: inherit;">We will use registered Spring service inside JSF managed-beans. For example consider <span style="font-family: "Courier New",Courier,monospace;">BikeDetails.java</span> managed bean. Previously we loaded certain bike in this way:</span><br />
<pre class="prettyprint"><span style="font-family: inherit;">package com.jsfsample.managedbeans;
// imports
@ManagedBean(name="bikeDetails")
@RequestScoped
public class BikeDetails {
private Integer bikeId;
private Bike bike;
public void loadBike(){
bike = BikeDataProvider.getInstance().getBikeById(bikeId);
}
...
}</span></pre>
<span style="font-family: inherit;">Now <span style="font-family: "Courier New",Courier,monospace;">BikeDataProvider.java</span> is not a singleton - it is a Spring service. <span style="font-family: "Courier New",Courier,monospace;">BikeDetails.java</span> managed bean is changed:</span><br />
<pre class="prettyprint lang-java"><span style="font-family: inherit;">package com.jsfsample.managedbeans;
// imports
@ManagedBean(name="bikeDetails")
@RequestScoped
public class BikeDetails {
private Integer bikeId;
private Bike bike;
@ManagedProperty("#{bikeDataProvider}")
private BikeDataProvider bikeDataProvider; // injected Spring service
public void loadBike(){
bike = bikeDataProvider.getBikeById(bikeId);
}
...
}</span></pre>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;">We used the name of registered service ("bikeDataProvider") to inject it into JSF managed bean (please note that we used here an interface as an instance variable, Spring injects its concrete implementation). This injection is possible thanks to modifications from step 4.</span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><br /></span>
<span style="font-family: inherit;"><br /></span>
<span style="font-family: inherit;">That's all. Other JSF managed beans responsible for displaying bikes' lists or adding a bike use mentioned Spring service in the same way.</span><br />
<span style="font-family: inherit;"><br /></span></div>
<div style="font-family: Verdana,sans-serif;">
</div>
<div style="font-family: Verdana,sans-serif;">
</div>
<div style="color: red;">
<span style="font-family: inherit;">------------------------------------------- </span></div>
<div style="color: red;">
<span style="font-family: inherit;">Download source files:</span></div>
<span style="font-family: inherit;">The complete working example of mentioned application which will contain all described issues, will be available in the last (second) article of this serie.</span><br />
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><br /></span></div>
<div style="font-family: Verdana,sans-serif;">
<br /></div>
Paweł Nieściorukhttp://www.blogger.com/profile/12640446237233312925noreply@blogger.com5tag:blogger.com,1999:blog-160318223199333541.post-71502288269350734172010-10-21T21:19:00.004+02:002018-12-16T15:05:55.214+01:00JSF 2.0 - New features (part 3 of 3)<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><b>Note: </b>below description uses <b>Eclipse Indigo</b>, <b>Tomcat 7.0.28</b> and <b>MyFaces 2.1.7</b>.<b> </b></span><br />
<span style="font-family: inherit;"><br /></span>
<b><span style="font-family: inherit;">Requirements:</span></b><br />
<ul>
<li><span style="font-family: inherit;">working example from <span id="goog_155504699"></span><a href="http://technology-for-human.blogspot.com/2010/10/jsf-20-new-features-part-2-of-3.html">previous post<span id="goog_155504700"></span></a> as a basis</span></li>
</ul>
<b><span style="font-family: inherit;">You will learn:</span></b><br />
<ul>
<li><span style="font-family: inherit;">support for Ajax</span></li>
<li><span style="font-family: inherit;">support for GET and creating bookmarkable URLs</span></li>
</ul>
<span style="font-family: inherit;"><br /></span>
<span style="font-family: inherit;">This is the third post about new features that could be found in JSF 2.0 comparing to JSF 1.x. In the <a href="http://technology-for-human.blogspot.com/2010/10/jsf-20-new-featuers-part-1-of-3.html">previous post</a> (part 1), I described templating mechanism and simplified navigation model. In the <a href="http://technology-for-human.blogspot.com/2010/10/jsf-20-new-features-part-2-of-3.html">second post</a> (part 2), I described resource loading mechanism. In this post I would like to focus on the built in Ajax support and on possibility to create bookmarkable URLs.</span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><br /></span>
<b><span style="font-family: inherit;">Ajax support.</span></b><br />
<span style="font-family: inherit;">Ajax is a very wide issue. I would like to show only one of the simplest example - refreshing part of the page after some action. The purpose of this example is to show that using simple Ajax does not require any additional configuration or libraries.</span><br />
<span style="font-family: inherit;">In the sample application I have a page <span style="font-family: "courier new" , "courier" , monospace;">bikesList.xhtml</span>. This page shows the bikes list in selected category (category is chosen in main menu on left). Some of the bikes have discount price. I have two buttons (above the displayed bikes list) acting as bike list filters. Pressing those buttons causes filtering the list and reloading it on the view. For example: if I press a button "Discount bikes" (in red box), only one bike will be shown on a list (Magnum bike with doscount price, marked by red box). If I press "All bikes" - all bikes from the category will be shown:</span><br />
<span style="font-family: inherit;"><br /></span>
<br />
<div style="color: red;">
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjB7GgYdvMWG4ZnNtQl-VE9WhOOe8gP9ap2x1NYEhydvORKXNZbs3iYEq62qt461fsheuYMg3N0F-qsip0aGInsGCbGkHK1MBG-mXAgpmuAqM3B0LbpXQGBixdcz7G1jGRXOyTLzB2Ya-b1/s1600/2010.10.21_1.PNG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><span style="font-family: inherit;"><img border="0" height="263" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjB7GgYdvMWG4ZnNtQl-VE9WhOOe8gP9ap2x1NYEhydvORKXNZbs3iYEq62qt461fsheuYMg3N0F-qsip0aGInsGCbGkHK1MBG-mXAgpmuAqM3B0LbpXQGBixdcz7G1jGRXOyTLzB2Ya-b1/s400/2010.10.21_1.PNG" width="400" /></span></a></div>
<span style="font-family: inherit;"><br /></span></div>
<div class="separator" style="clear: both; text-align: center;">
</div>
<span style="font-family: inherit;">The idea is to reload the bike list after pressing the button, without reloading the whole page. This is done by Ajax call which is "hooked" to the buttons:</span><br />
<pre class="prettyprint"><span style="font-family: inherit;"><ui:define name="content">
<h:commandButton actionListener="#{bikesListBean.showAllBikes}" value="#{msg['bikes.filter.all']}">
<f:ajax render="bikesTable" />
</h:commandButton>
<h:commandButton actionListener="#{bikesListBean.showDiscountBikes}" value="#{msg['bikes.filter.discount']}">
<f:ajax render="bikesTable" />
</h:commandButton>
<h:dataTable id="bikesTable" value="#{bikesListBean.bikesList}" var="b">
...
</ui:define>
</span></pre>
<span style="font-family: inherit;">Ajax call defined by <span style="font-family: "courier new" , "courier" , monospace;"><f:ajax render="bikesTable" /></span> causes reloading the corresponding part of thw page with <span style="font-family: "courier new" , "courier" , monospace;">id=bikesTable</span>. In our case this is the list <span style="font-family: "courier new" , "courier" , monospace;"> <h:dataTable id="bikesTable" ... ></span> which shows all or filtered bikes. Simple - isn't it?</span><br />
<span style="font-family: inherit;"><br /></span>
<b><span style="font-family: inherit;">Creating bookmarkable URLs (GET support).</span></b><br />
<span style="font-family: inherit;">Why do I need GET? Consider this situation: I found interesting bike in the Bike Shop. I would like to get the URL and send it to someone in order to show what I found. Quite normal thing isn't it? Unfortunately not possible in JSF 1.x because of lack of GET support. JSF 1.x is POST-centric, so passing parameters in the URL wiht GET was not possible - so I have no way to save the URL.</span><br />
<span style="font-family: inherit;">JSF 2.0 provided GET support by introducing so-called "view parameters". A page which uses a special component for view parameters, is able to catch incoming view parameters (they are in the URL) and update page model (i.e. fields in managed bean) with their values. Moreover standard conversion and validation of the incoming parameters is possible - just like for the POST data.</span><br />
<span style="font-family: inherit;"><br /></span>
<span style="font-family: inherit;">How it work in real example? Let's go back to our sample application. We have a page named <span style="font-family: "courier new" , "courier" , monospace;">bikesList.xhtml</span> which shows all bikes. We also have a page named <span style="font-family: "courier new" , "courier" , monospace;">bikeDetails.xhtml</span> which shows the detail of selected bike. I would like to see the particular bike and take the URL with it and send it to someone. So the <span style="font-family: "courier new" , "courier" , monospace;">bikeDetails.xhtml</span> page will contain view parameter component:</span><br />
<pre class="prettyprint"><span style="font-family: inherit;"><f:metadata>
<f:viewParam name="bikeId" value="#{bikeDetails.bikeId}"/>
<f:event type="preRenderView" listener="#{bikeDetails.loadBike}"/>
</f:metadata></span></pre>
<span style="font-family: inherit;">Page expects the URL parameter named <span style="font-family: "courier new" , "courier" , monospace;">bikeId</span>, then value of this parameter is copied into the model to the field <span style="font-family: "courier new" , "courier" , monospace;">#{bikeDetails.bikeId}</span>. For now skip <span style="font-family: "courier new" , "courier" , monospace;"><f:event .../></span> component - I will back to it at the end.</span><br />
<span style="font-family: inherit;">Where those parameters are created and passed to the URL? JSF 2.0 provides two components allowing to add GET parameters to the target URL: <span style="font-family: "courier new" , "courier" , monospace;"><h:button /></span> and<span style="font-family: "courier new" , "courier" , monospace;"> <h:link /></span>(they work similar to the POST-centric <span style="font-family: "courier new" , "courier" , monospace;"><h:commandButton /></span> and<span style="font-family: "courier new" , "courier" , monospace;"> <h:commandLink</span>). In our example <span style="font-family: "courier new" , "courier" , monospace;">bikesList.xhtml</span> page uses <span style="font-family: "courier new" , "courier" , monospace;"><h:button /> </span>for each presented bike to construct the button which navigates us to <span style="font-family: "courier new" , "courier" , monospace;">bikeDetails.xhtml </span>and sets bikeId as a parameter:</span><br />
<pre class="prettyprint"><span style="font-family: inherit;"><h:dataTable id="bikesTable" value="#{bikesListBean.bikesList}" var="b">
...
<h:button outcome="bikeDetails" value="#{msg['bikes.list.seebike']}">
<f:param name="bikeId" value="#{b.id}"/>
</h:button>
...
</h:dataTable></span></pre>
<span style="font-family: inherit;">Notice <span style="font-family: "courier new" , "courier" , monospace;">outcome</span> attribute and its value - it points to the target page where view parameters component is used. <a href="http://technology-for-human.blogspot.com/2010/10/jsf-20-new-featuers-part-1-of-3.html">Simplified navigation</a> is used here. Generated URL looks like this:</span><br />
<span style="font-family: inherit;"><br /></span>
<b style="font-family: "Courier New",Courier,monospace;"><span style="font-family: inherit;">http://localhost:8080/JSF2Features/faces/bikeDetails.xhtml?bikeId=5</span></b><br />
<span style="font-family: inherit;"><br /></span>
<span style="font-family: inherit;">We have bokkmarkable URL which can be saved and sent to other people and used later. </span><br />
<span style="font-family: inherit;"><br /></span>
<span style="font-family: inherit;">Solution with view parameters on <span style="font-family: "courier new" , "courier" , monospace;">bikeDetails.xhtml</span> uses another new feature in JSF 2.0: system events. We register a special listener using the tag </span><br />
<pre class="prettyprint"><span style="font-family: inherit;"><f:event type="preRenderView" listener="#{bikeDetails.loadBike}"/></span></pre>
<span style="font-family: inherit;"><br /></span>
<span style="font-family: inherit;">The listener is executed before the view is rendered. What is the benefit? When a view is reached, view parameter is copied into the managed bean and listener is executed and <b>after that</b> the whole view (page) is rendered. Our listener uses passed <span style="font-family: "courier new" , "courier" , monospace;">bikeId</span> to load the selected bike from repository like database. When the page is rendered, the proper bike is loaded from repository and ready to be displayed - we do not have to wait for loading the data.</span><br />
<span style="font-family: inherit;"><br /></span>
<span style="font-family: inherit;">That's all. We are ready to test the application. After deploying application on the server and starting the server, we have to open a browser and type in URL:</span><br />
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><br /></span></div>
<div style="font-family: Verdana,sans-serif;">
<b><span style="font-family: inherit;">http://localhost:8080/JSF2Features</span></b></div>
<span style="font-family: inherit;"><br /></span>
<span style="font-family: inherit;"><br /></span>
<br />
<div style="color: red;">
<span style="font-family: inherit;">------------------------------------------- </span></div>
<div style="color: red;">
<span style="font-family: inherit;">Download source files:</span></div>
<span style="font-family: inherit;"><br /></span>
<span style="font-family: inherit;"><b>Note:</b> make sure that <a href="http://technology-for-human.blogspot.com/2009/04/instalacja-java-jdk-prawie-bezbolesna.html">Java</a>, <a href="http://technology-for-human.blogspot.com/2009/04/instalacja-i-konfiguracja-ide-eclipse.html">Eclipse</a> and <a href="http://technology-for-human.blogspot.com/2009/05/konfiguracja-tomcata-w-eclipse.html">Tomcat</a> are properly installed and configured for running the project (additional configuration may be required if different directories are used).</span><br />
<span style="font-family: inherit;"><br /></span>
<span style="font-family: inherit;">Eclipse complete sample project is <a href="https://drive.google.com/open?id=13mjdTe98HteJ92i15uiifJMnWy9awSTQ">here</a> (with all required libraries). The sample project is a ready to run application which contains all described JSF 2.0 features in <a href="http://technology-for-human.blogspot.com/2010/10/jsf-20-new-featuers-part-1-of-3.html">first</a>, <a href="http://technology-for-human.blogspot.com/2010/10/jsf-20-new-features-part-2-of-3.html">second</a> and this (third) post. You can also download a war file located <a href="https://drive.google.com/open?id=1z_EKcGIMme13bNozYynLhOhjDVqFOxmf">here</a> (just copy it inside <span style="font-family: "courier new" , "courier" , monospace;">webapps</span> folder in Your Tomcat and start Tomcat with the script <span style="font-family: "courier new" , "courier" , monospace;">startup.bat</span>)</span><br />
<div style="font-family: verdana, sans-serif;">
<span style="font-family: "courier new" , "courier" , monospace;"></span></div>
</div>
Paweł Nieściorukhttp://www.blogger.com/profile/12640446237233312925noreply@blogger.com3tag:blogger.com,1999:blog-160318223199333541.post-39306381035966809222010-10-20T20:28:00.003+02:002018-12-16T13:43:00.401+01:00JSF 2.0 - New features (part 2 of 3)<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><b>Note: </b>below description uses <b>Eclipse Indigo</b>, <b>Tomcat 7.0.28</b> and <b>MyFaces 2.1.7</b>.<b> </b></span><br />
<span style="font-family: inherit;"><br /></span>
<b><span style="font-family: inherit;">Requirements:</span></b><br />
<ul>
<li><span style="font-family: inherit;">working example from <a href="http://technology-for-human.blogspot.com/2010/10/jsf-20-new-featuers-part-1-of-3.html">previous post</a> as a basis<br />
</span></li>
</ul>
<b><span style="font-family: inherit;">You will learn:</span></b><br />
<ul>
<li><span style="font-family: inherit;">resource loading</span></li>
</ul>
<span style="font-family: inherit;">This is the second post about new features that could be found in JSF 2.0 comparing to JSF 1.x. In the <a href="http://technology-for-human.blogspot.com/2010/10/jsf-20-new-featuers-part-1-of-3.html">previous post</a> (part 1), I described templating mechanism and simplified navigation model. In this post I would like to focus on the improved resource loading and its capabilities in JSF 2.0.</span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><br /></span></div>
<div style="font-family: Verdana,sans-serif;">
<b><span style="font-family: inherit;">Resource loading.</span></b></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;">What are resources in terms of a web application? It can be images files, JavaScript script files or CSS files used on web pages in the application - so some external files accessible by special tags in the page source. In the JSF application we also name as resources message bundle files. </span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;">Just as a short reminder for message bundles:</span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><b>1.</b> We create message bundle files for each language we would like to support. Then we add some lines into the configuration in faces-config.xml where we define what language will be supported (<span style="font-family: "Courier New",Courier,monospace;"><locale-config></span> section) and we define EL access object (<span style="font-family: "Courier New",Courier,monospace;"><var>msg</var></span>) for the created message bundle files:</span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><br /></span>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiuQVDvdcnAkGPNuCitE-Mn9MqhyLKCWd2Arh_ijcY1JcVTk_Q0mSy8qGILe9HmbxcG4IRNpb1xjxyu48FBC_LNDQm6W2MQEtAEiS7YrK0qIPtj9YiVyy1FQFJeL2OybJXg45CdVUW94eV8/s1600/2010.10.20_1.PNG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><span style="font-family: inherit;"><img border="0" height="218" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiuQVDvdcnAkGPNuCitE-Mn9MqhyLKCWd2Arh_ijcY1JcVTk_Q0mSy8qGILe9HmbxcG4IRNpb1xjxyu48FBC_LNDQm6W2MQEtAEiS7YrK0qIPtj9YiVyy1FQFJeL2OybJXg45CdVUW94eV8/s400/2010.10.20_1.PNG" width="400" /></span></a></div>
<span style="font-family: inherit;"><br /></span></div>
<div class="separator" style="clear: both; text-align: center;">
</div>
<div style="font-family: Verdana,sans-serif;">
</div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><b>2. </b> We use message from the bundle files by calling defined EL accessor in the page source, for example:</span></div>
<pre class="prettyprint" style="font-family: "Courier New",Courier,monospace;"><span style="font-family: inherit;"><h:outputText value="#{msg['top.name']}" /></span></pre>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;">This approach gives us the most important benefit: localization of the application. Based on the browser's language, suitable message bundle file is used (EN or PL in above example).</span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><br /></span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;">So far so good - nothing new is here when comparing to JSF 1.x. But what about other resources like JS, CSS or image files? JSF 2.0 can handle those resources in a more intelligent way than before - a special <a href="https://javaserverfaces.dev.java.net/nonav/docs/2.0/javadocs/javax/faces/application/ResourceHandler.html">ResourceHandler</a> is used for load resources from some predefined locations. According to the documentation resources are expected to be placed in:</span><br />
<span style="font-family: inherit;"><br /></span>
<div style="font-family: "Courier New",Courier,monospace;">
<span style="font-family: inherit;">WebRoot/resources/<resourceIdentifier> </span></div>
<span style="font-family: inherit;"><br /></span>
<span style="font-family: inherit;">WebRoot is a root of web application - in the Eclipse generated web projects it is the directory named "WebContent". Part <span style="font-family: "Courier New",Courier,monospace;">resourceIdentifier</span> have this structure:</span><br />
<span style="font-family: inherit;"><br /></span>
<div>
<span style="font-family: inherit;"><span style="font-family: "Courier New",Courier,monospace;">[localePrefix/][libraryName/][libraryVersion/]resourceName[/resourceVersion] </span> </span></div>
</div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><br /></span>
<span style="font-family: inherit;"><span style="font-family: "Courier New",Courier,monospace;">resourceIdentifier</span> defines the subdirectory structure inside <span style="font-family: "Courier New",Courier,monospace;">WebRoot/resources/directory</span>. And the most important part: <span style="font-family: "Courier New",Courier,monospace;">localePrefix</span> means that<b> resources can be localized based on the browser's language</b>, like message bundles. Part <span style="font-family: "Courier New",Courier,monospace;">libraryName</span> can be use to group resources by their type in different directories, for example "css", "images", "scripts".</span><br />
<span style="font-family: inherit;"><br /></span>
<span style="font-family: inherit;">Let' see how it work in a sample application. As I wrote in previous <a href="http://technology-for-human.blogspot.com/2010/10/jsf-20-new-featuers-part-1-of-3.html">post</a>, we used <span style="font-family: "Courier New",Courier,monospace;">shopTemplate.xhtml</span> file as a template page for all pages in application. The second purpose of this file (next to defining reusable parts) is to separate the structure from the presentation - template page will include all CSS files and common images files. <b>We will use new ResourceHandler in order to load some CSS and images suitable for current browser language:</b></span><br />
<ul>
<li><span style="font-family: inherit;">images: we have two pictures (<span style="font-family: "Courier New",Courier,monospace;">bike_logo.jpg</span> and <span style="font-family: "Courier New",Courier,monospace;">flag.gif</span>) in sample application visible in the header - they will change depending on current browser language</span></li>
<li><span style="font-family: inherit;">CSS: depending on current browser language, different CSS will be loaded. The only difference between loaded CSS's is footer color (just to show that CSS is really changed) </span></li>
</ul>
<span style="font-family: inherit;">According to the documentation we have to create directory structure for mentioned resources and put different resource there:</span><br />
<span style="font-family: inherit;"><br /></span>
<div style="color: red;">
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhj7ATAhXfnqQGRFlDrW1hnlnbFd3FmXYyOQfc8VCHvMWzrIfbwxe6qrpM3wo24J5aWcLT6gruBkGNdM7eTvsWDN3_5_gOc8iElObXepM2yxFu3uy_Kq_v3WpyTYeSfrVWCEtv6EFjSmjF3/s1600/2010.10.20_2.PNG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><span style="font-family: inherit;"><img border="0" height="400" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhj7ATAhXfnqQGRFlDrW1hnlnbFd3FmXYyOQfc8VCHvMWzrIfbwxe6qrpM3wo24J5aWcLT6gruBkGNdM7eTvsWDN3_5_gOc8iElObXepM2yxFu3uy_Kq_v3WpyTYeSfrVWCEtv6EFjSmjF3/s400/2010.10.20_2.PNG" width="218" /></span></a></div>
<span style="font-family: inherit;"><br /></span></div>
<div class="separator" style="clear: both; text-align: center;">
</div>
<span style="font-family: inherit;"><br /></span>
<span style="font-family: inherit;">How to load such resources on the page? First we have to perform additional configuration to enable loading resources using proper <span style="font-family: "Courier New",Courier,monospace;"> localePrefix</span> ("pl" and "en" on the screenshot above). We have to create a special localized entry named <span style="font-family: "Courier New",Courier,monospace;">javax.faces.resource.localePrefix</span> in a resource-bundle file. This file <b>must be configured as message-bundle in faces-config.xml</b> - we <b>can not use resource bundles</b> (used for localized messages as shown above) to put entry there:</span><br />
<span style="font-family: inherit;"><br /></span>
<div style="color: red;">
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj-GLBu5vcDKQe1WVPoGKelzGK2HTem_6phvZ1_rm0czzNRzssaKfWEYO8dFHErHZYg2G7-ZRSTanfwhOujaQVEpgI-ZfPN_r5uhDER9fQrSuo1FAL_Fh_gh2UhalhLnkAv1vgYmmy0dKQj/s1600/2010.10.20_3.PNG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><span style="font-family: inherit;"><img border="0" height="256" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj-GLBu5vcDKQe1WVPoGKelzGK2HTem_6phvZ1_rm0czzNRzssaKfWEYO8dFHErHZYg2G7-ZRSTanfwhOujaQVEpgI-ZfPN_r5uhDER9fQrSuo1FAL_Fh_gh2UhalhLnkAv1vgYmmy0dKQj/s400/2010.10.20_3.PNG" width="400" /></span></a></div>
<span style="font-family: inherit;"><br /></span></div>
<div class="separator" style="clear: both; text-align: center;">
</div>
<span style="font-family: inherit;">Now we can use the resources on the <span style="font-family: "Courier New",Courier,monospace;">shopTemplate.xhtml</span> page:</span><br />
<span style="font-family: inherit;"><br /></span>
<div style="color: red;">
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgTWcRyaD1MhOpi8eBfv1Un6ZzM-Rbvne4KThKVAdGoAmCaAmDJ0w-SWEPb3fpwQQ7QGxCjXcbID69-77UcyqX12YiRGxY2nR9Pqp6r4E1Wox5pHQmsc4P2G2IbfvwVa5jzhh95hM0XOMnZ/s1600/2010.10.20_4.PNG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><span style="font-family: inherit;"><img border="0" height="208" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgTWcRyaD1MhOpi8eBfv1Un6ZzM-Rbvne4KThKVAdGoAmCaAmDJ0w-SWEPb3fpwQQ7QGxCjXcbID69-77UcyqX12YiRGxY2nR9Pqp6r4E1Wox5pHQmsc4P2G2IbfvwVa5jzhh95hM0XOMnZ/s400/2010.10.20_4.PNG" width="400" /></span></a></div>
<span style="font-family: inherit;"><br /></span></div>
<div class="separator" style="clear: both; text-align: center;">
</div>
<span style="font-family: inherit;">Please note that we use here a new JSF 2.0 tag:</span><br />
<pre class="prettyprint" style="font-family: "Courier New",Courier,monospace;"><span style="font-family: inherit;"><h:outputStylesheet library="css" name="layout.css" /></span></pre>
<span style="font-family: inherit;">Attributes <span style="font-family: "Courier New",Courier,monospace;">library</span> and <span style="font-family: "Courier New",Courier,monospace;">name</span> correspond to the <span style="font-family: "Courier New",Courier,monospace;">libraryName</span> and <span style="font-family: "Courier New",Courier,monospace;">resourceName </span>from <span style="font-family: "Courier New",Courier,monospace;"><resourceIdentifier></span> respectively.</span><br />
<span style="font-family: inherit;"><br /></span>
<span style="font-family: inherit;">If we want to load JS scripts we have to use additional JSF 2.0 tag: <span style="font-family: "Courier New",Courier,monospace;"><h:outputScript ... /></span> wich has additional attribute named <span style="font-family: "Courier New",Courier,monospace;">target</span> - specifying where to render script link.</span><br />
<span style="font-family: inherit;"><br /></span>
<span style="font-family: inherit;">Loading images with standard tag <span style="font-family: "Courier New",Courier,monospace;"><h:graphicImage ... /></span> is possible in two ways:</span><br />
<ul>
<li><span style="font-family: inherit;">using attributes library and name correspond to the libraryName and resourceName from <span style="font-family: "Courier New",Courier,monospace;"><resourceIdentifier></span> respectively (like for CSS)</span></li>
<li><span style="font-family: inherit;">using special EL expression <span style="font-family: "Courier New",Courier,monospace;">"#{resource['images:bike_logo.jpg']}"</span> (wartch out for letters: EL has resource word but in the WebContent there is a resource<b>s</b> directory!)</span></li>
</ul>
<span style="font-family: inherit;"><br /></span>
<span style="font-family: inherit;"><b>Note about testing:</b> in order to test if images and CSS (footer color) changes when browser language is changed, I recommend clean the browser cache and restart it completely before testing (sometimes image cache prevents from changing the image). The test result should be:</span><br />
<span style="font-family: inherit;"><br /></span>
<span style="font-family: inherit;">PL version:</span><br />
<span style="font-family: inherit;"><br /></span>
<div style="color: red;">
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiC72tqoCAPuLNZVR69mLs5XjRi5tcVO5SWkbVuZ3RZRu36_tPif4fBGbBwskzg3ZN57bWlZM1gq2pzLkCIxWvwUgvrrMc-VY62lZ7WyWDzDVrsmxvPqLl4sM-9frci3FdpR0PMAcfyxPt3/s1600/2010.10.20_5.PNG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><span style="font-family: inherit;"><img border="0" height="266" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiC72tqoCAPuLNZVR69mLs5XjRi5tcVO5SWkbVuZ3RZRu36_tPif4fBGbBwskzg3ZN57bWlZM1gq2pzLkCIxWvwUgvrrMc-VY62lZ7WyWDzDVrsmxvPqLl4sM-9frci3FdpR0PMAcfyxPt3/s400/2010.10.20_5.PNG" width="400" /></span></a></div>
<span style="font-family: inherit;"><br /></span></div>
<div class="separator" style="clear: both; text-align: center;">
</div>
<span style="font-family: inherit;">EN version:</span><br />
<div style="color: red;">
<span style="font-family: inherit;"><br /></span></div>
<div style="color: red;">
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh9JOf9zwGv4KOch2hmczMHlyKB3uaEZjdXCO38VyUUiQCX3GHLexHyBT2y9eeSN4tFS5MiFKOBeuGUZ0ELyyWkgQUDjZcDdfGOoCEMNpurTGQMnmNthzvQMq7ZPn0Kx5xfRwExqibibIMN/s1600/2010.10.20_6.PNG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><span style="font-family: inherit;"><img border="0" height="256" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh9JOf9zwGv4KOch2hmczMHlyKB3uaEZjdXCO38VyUUiQCX3GHLexHyBT2y9eeSN4tFS5MiFKOBeuGUZ0ELyyWkgQUDjZcDdfGOoCEMNpurTGQMnmNthzvQMq7ZPn0Kx5xfRwExqibibIMN/s400/2010.10.20_6.PNG" width="400" /></span></a></div>
<span style="font-family: inherit;"><br /></span></div>
<div class="separator" style="clear: both; text-align: center;">
</div>
</div>
<div style="color: red;">
<span style="font-family: inherit;">------------------------------------------- </span></div>
<div style="color: red;">
<span style="font-family: inherit;">Download source files:</span></div>
<div style="font-family: Verdana,sans-serif;">
</div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;">The complete working example of mentioned application which will contain all described features, will be available in the last (third) article of this serie.</span><br />
<br /></div>
<div style="font-family: Verdana,sans-serif;">
</div>
<div style="font-family: Verdana,sans-serif;">
</div>
Paweł Nieściorukhttp://www.blogger.com/profile/12640446237233312925noreply@blogger.com1tag:blogger.com,1999:blog-160318223199333541.post-16547810309953222572010-10-17T20:39:00.007+02:002018-12-16T13:30:45.403+01:00JSF 2.0 - New features (part 1 of 3)<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><b>Note: </b>below description uses <b>Eclipse Indigo</b>, <b>Tomcat 7.0.28</b> and <b>MyFaces 2.1.7</b>.<b> </b></span><br />
<span style="font-family: inherit;"><br /></span>
<b><span style="font-family: inherit;">Requirements:</span></b><br />
<ul>
<li><span style="font-family: inherit;">working "Hello World" example in JSF 2.0 as a basis (from <a href="http://technology-for-human.blogspot.com/2010/09/jsf-20-hello-world-in-eclipse-with.html">here</a>)</span></li>
</ul>
<b><span style="font-family: inherit;">You will learn:</span></b><br />
<ul>
<li><span style="font-family: inherit;">templating with Facelets</span></li>
<li><span style="font-family: inherit;">simplified page navigation</span></li>
</ul>
<span style="font-family: inherit;">In the previous <a href="http://technology-for-human.blogspot.com/2010/09/jsf-20-hello-world-in-eclipse-with.html">post</a> I showed how to create classic "Hello World" application in JSF 2.0. The example was created in the simplest possible way. I focused on generating and setting up working and ready to use project in Eclipse. Created application did not differ much from similar "Hello World" applications in JSF 1.2, because I did not use JSF 2.0 built-in featues. Now it is a time to show what new features are in JSF 2.0 and how they can improve development web applications.<b> </b></span></div>
<span style="font-family: inherit;"><br /></span>
<div style="font-family: Verdana,sans-serif;">
<b><span style="font-family: inherit;">Sample application: </span></b></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;">In order to go higher than "Hello World" level we will create fully functional mini application which will serve as an example for mentioned JSF 2.0 features.</span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;">Let's assume that we are creating a web application for bicycle shop. Our application should present a list of available bikes. Bikes will be divided because of their type like MTB, Trekking and Cross bikes. In addition bike list will have a filter allowing to display only bikes with a special discount price. User should be able to display detailed information about selected bike from the presented list. We will use those pages in application:</span></div>
<ul style="font-family: Verdana,sans-serif;">
<li><span style="font-family: inherit;"><span style="font-family: "Courier New",Courier,monospace;">bikesShop.xhtml</span> - start page presenting information about bike shop</span></li>
<li><span style="font-family: inherit;"><span style="font-family: "Courier New",Courier,monospace;">bikesList.xhtml</span> - page presenting bikes of given type</span></li>
<li><span style="font-family: inherit;"><span style="font-family: "Courier New",Courier,monospace;">bikeDetails.xhtml</span> - page presenting detailed information about bike displayed on <span style="font-family: "Courier New",Courier,monospace;">bikesList.xhtml</span></span></li>
</ul>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;">Under the hood we will use those classes:</span><br />
<ul>
<li><span style="font-family: inherit;"><span style="font-family: "Courier New",Courier,monospace;">BikeDataProvider.java</span> - singleton, acts as service which belongs to business logic. Responsible for loading bikes of certain type and loading single bike with its details. For simplicity all bikes instances are created and stored inside that class.</span></li>
<li> <span style="font-family: inherit;"><span style="font-family: "Courier New",Courier,monospace;">Bike.java</span><i> - </i>a class from the model representing single bike instance. A list of bike instances is created and used inside <span style="font-family: "Courier New",Courier,monospace;">BikeDataProvider.java</span>.</span></li>
<li><span style="font-family: inherit;"><span style="font-family: "Courier New",Courier,monospace;">BikeDetails.java, BikesList.java</span> - managed beans used for <span style="font-family: "Courier New",Courier,monospace;">bikeDetails.xhtml</span><i> </i>and <span style="font-family: "Courier New",Courier,monospace;">bikesList.xhtml</span> respectively. They call <span style="font-family: "Courier New",Courier,monospace;">BikeDataProvider.java</span> for loading bikes list or single bike. </span></li>
</ul>
<span style="font-family: inherit;"><br /></span>
<span style="font-family: inherit;">Web application will have popular and standard layout - header on top, menu on left, content on righ, footer on bottom. Header will have shop's logo and name, menu will have bike types listed, content will have some information about the shop and will display bikes list for given type, footer will be empty with some background color. The whole application will look like this:</span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><br /></span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><span style="font-family: "Courier New",Courier,monospace;">bikesShop.xhtml</span>:</span><br />
<span style="font-family: inherit;"><br /></span>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEindzeaIVCLQBkOq9qm_b1DMUXho8UT6vq9UftubS7aZoP9-sbJCJBFjD-57LQCxWRjgnG5VBPUly_8yduaNHM9QEG2Pcp3JxrcmkiTe2ORfFxplatdoapmYisiE5IQdcHAeqGPbX2gDNOY/s1600/2010.10.17_1.PNG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><span style="font-family: inherit;"><img border="0" height="204" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEindzeaIVCLQBkOq9qm_b1DMUXho8UT6vq9UftubS7aZoP9-sbJCJBFjD-57LQCxWRjgnG5VBPUly_8yduaNHM9QEG2Pcp3JxrcmkiTe2ORfFxplatdoapmYisiE5IQdcHAeqGPbX2gDNOY/s320/2010.10.17_1.PNG" width="320" /></span></a></div>
<span style="font-family: inherit;"><br /></span></div>
<div class="separator" style="clear: both; text-align: center;">
</div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><span style="font-family: "Courier New",Courier,monospace;">bikesList.xhtml</span>:</span><br />
<span style="font-family: inherit;"><br /></span>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgTHwHT-WLODPKScC1lKYJFuWpCltotWTROffNndTTkJbH6kCDcnqUGPXoOkMCa-6uQvTgyZL4LMKFmcEzMANjtlb0fYg4IebA8Am7yf904rTjMiEqBuZ8ABeCjZvNOLOuOGgXGnNcZmIN7/s1600/2010.10.17_2.PNG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><span style="font-family: inherit;"><img border="0" height="210" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgTHwHT-WLODPKScC1lKYJFuWpCltotWTROffNndTTkJbH6kCDcnqUGPXoOkMCa-6uQvTgyZL4LMKFmcEzMANjtlb0fYg4IebA8Am7yf904rTjMiEqBuZ8ABeCjZvNOLOuOGgXGnNcZmIN7/s320/2010.10.17_2.PNG" width="320" /></span></a></div>
<span style="color: red; font-family: inherit;"><br /> </span></div>
<div class="separator" style="clear: both; text-align: center;">
</div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><span style="font-family: "Courier New",Courier,monospace;">bikesDetails.xhtml</span>:</span></div>
<div class="separator" style="clear: both; text-align: left;">
<span style="font-family: inherit;"><br /></span></div>
<div class="separator" style="clear: both; text-align: left;">
</div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg6p5VCk8ngnO34vKjBhBfuo5tK-xcCjT_-76_gx2MSHCSGue3Xjaw2ZrxYTp_haPZ1mYQ95JTvMcldkNBUqLfoDuXKz4RrcoSHOL6Aea0Wy2oHabxwaEUW8xttyaHyCjBtHdJ4Ot7BEdC_/s1600/2010.10.17_3.PNG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><span style="font-family: inherit;"><img border="0" height="210" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg6p5VCk8ngnO34vKjBhBfuo5tK-xcCjT_-76_gx2MSHCSGue3Xjaw2ZrxYTp_haPZ1mYQ95JTvMcldkNBUqLfoDuXKz4RrcoSHOL6Aea0Wy2oHabxwaEUW8xttyaHyCjBtHdJ4Ot7BEdC_/s320/2010.10.17_3.PNG" width="320" /></span></a></div>
<div class="separator" style="clear: both; text-align: left;">
<span style="font-family: inherit;"><br /></span></div>
<div style="font-family: Verdana,sans-serif;">
</div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><b>Facelets - templating.</b> </span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;">One of the key feature of Facelets is ability to create page templates. Have a look at our sample applications screenshot above - all of them have common content like header, left menu and footer. If we had JSF application based on JSP pages without using Facelets, those elements would be included separately into source code of each web page. Imagine small change in the header - it may become a maintenance nightmare because we have to change source code of all pages where header is visible. With the Facelets is it possible to extract common content and put it into <b>one page template</b>. The template has special sections where variable content will be displayed - pages which use the template "injects" to those sections their specific content.</span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><br /></span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;">For our application we will create a template page named <b style="font-family: "Courier New",Courier,monospace;">shopTemplate.xhtml</b> which will act as a template for three pages visible above (<span style="font-family: "Courier New",Courier,monospace;">bikesShop.xhtml, bikesList.xhtml, bikeDetails.xhtml</span>). The whole page layout common for all pages will be defined inside the single template page. This gives us another advantage - we can follow one of the best practises in designing web pages here - separate the structure from the presentation. The template will contain only the pages structure while the whole presentation will be placed in separate CSS file used by template. The source code for the template will look like this:</span></div>
<pre class="prettyprint" style="font-family: Verdana,sans-serif;"><span style="font-family: inherit;"><?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core">
<h:head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Bike Shop 2.0</title>
</h:head>
<h:body>
<div id="container">
<div id="top">
<!-- logo and name goes here -->
</div>
<div id="leftnav">
<h:form>
<!-- links goes here -->
</h:form>
</div>
<div id="content">
<ui:insert name="content" />
</div>
<div id="footer">
####
</div>
</div>
</h:body>
</html>
</span></pre>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><br /></span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;">Please note the element <b><span style="font-family: "Courier New",Courier,monospace;"><ui:insert name="content" /></span> </b>inside the div named "content".<b> </b>It represents variable content which will be displayed in this place. Pages <span style="font-family: "Courier New",Courier,monospace;">bikesShop.xhtml, </span><span style="font-family: "Courier New",Courier,monospace;">bikesList.xhtml</span> and <span style="font-family: "Courier New",Courier,monospace;">bikeDetails.xhtml</span> will "inject" here their content using special syntax. Let's have a look how <span style="font-family: "Courier New",Courier,monospace;">bikesShop.xhtml</span> page display its content using the template. Here is the code for <span style="font-family: "Courier New",Courier,monospace;">bikesShop.xhtml</span>:</span></div>
<pre class="prettyprint" style="font-family: Verdana,sans-serif;"><span style="font-family: inherit;"><?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core">
<ui:composition template="shopTemplate.xhtml">
<ui:define name="content">
Lorem ipsum dolor sit amet...
</ui:define>
</ui:composition>
</html>
</span></pre>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><br /></span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;">First note that <span style="font-family: "Courier New",Courier,monospace;">bikesShop.xhtml</span> page have no <head> or <body> tags - they are defined in the template. A special element <b><span style="font-family: "Courier New",Courier,monospace;"><ui:composition template="shopTemplate.xhtml"></span> </b>tells the page to use the mentioned template.</span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;">As I wrote, the template has a special section for inserting the variable content (<span style="font-family: "Courier New",Courier,monospace;"><ui:insert name="content" /></span>). The page <span style="font-family: "Courier New",Courier,monospace;">bikesShop.xhtml</span><i> </i>defines content to be placed into that section by using the tag <b style="font-family: "Courier New",Courier,monospace;"><ui:define name="content"> ... </ui:define></b> - note that attribute <span style="font-family: "Courier New",Courier,monospace;">name="content"</span> is the same for <span style="font-family: "Courier New",Courier,monospace;"><ui:insert/></span> and <span style="font-family: "Courier New",Courier,monospace;"><ui:define /></span>. Pages <span style="font-family: "Courier New",Courier,monospace;">bikesList.xhtml</span> and <span style="font-family: "Courier New",Courier,monospace;">bikeDetails.xhtml</span><i> </i>use the same mechanism (they have the same <span style="font-family: "Courier New",Courier,monospace;"><ui:define /></span> tag in their source).</span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;">That's all about templates. </span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><br /></span></div>
<div style="font-family: Verdana,sans-serif;">
<b><span style="font-family: inherit;">Simplified page navigation.</span></b></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;">Every page navigation in JSF 1.x required a proper entry in <span style="font-family: "Courier New",Courier,monospace;">faces-config.xml</span> file. It was something like this:</span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><br /></span></div>
<pre class="prettyprint" style="font-family: Verdana,sans-serif;"><span style="font-family: inherit;"><navigation-rule>
<description>Welcome page to message page</description>
<from-view-id>/index.jsp</from-view-id>
<navigation-case>
<from-outcome>helloMessage</from-outcome>
<to-view-id>/message.jsp</to-view-id>
</navigation-case>
</navigation-rule>
</span></pre>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><br /></span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;">JSF 2.0 provides a simplified navigation model - we <b>do not need any entries</b> (navigation rules) in the <span style="font-family: "Courier New",Courier,monospace;">faces-config.xml</span> file. Why?</span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;">Suppose we have a method used for navigation which returns some string value. This string value (known as <span style="font-family: "Courier New",Courier,monospace;">outcome</span>) is taken by a navigation handler and the handler checks for navigation rules in <span style="font-family: "Courier New",Courier,monospace;">faces-config.xml</span> which have defined the same <span style="font-family: "Courier New",Courier,monospace;">from-outcome</span> value. When the rule is found, it is applied and a proper navigation is done. This is how it worked in JSF 1.x and how it works in JSF 2.0. But JSF 2.0 navigation handler does additional operation here: if no matching <span style="font-family: "Courier New",Courier,monospace;">from-outcome</span> value is found in <span style="font-family: "Courier New",Courier,monospace;">faces-config.xml</span> (in other words: there is no navigation rule to apply), handler checks also existing pages names (view identifiers). If existing page name matches returned <span style="font-family: "Courier New",Courier,monospace;">outcome</span> value, the navigation is done to that page.</span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;">For example if we have a navigation method in some backing bean:</span></div>
<pre class="prettyprint" style="font-family: Verdana,sans-serif;"><span style="font-family: inherit;">public String getBikes(){
bikesList = ... // load some bikes
return "bikesList";
}
</span></pre>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;">and there is a page named <span style="font-family: "Courier New",Courier,monospace;">bikesList.xhtml</span>, invoking this method will cause the navigation to that page - without defining proper navigation rule in <span style="font-family: "Courier New",Courier,monospace;">faces-config.xml</span>.</span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><br /></span></div>
<div style="color: red;">
<span style="font-family: inherit;">------------------------------------------- </span></div>
<div style="color: red;">
<span style="font-family: inherit;">Download source files:</span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><br /></span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;">The complete working example of mentioned application which will contain all described features, will be available in the last (third) article of this serie.</span></div>
Paweł Nieściorukhttp://www.blogger.com/profile/12640446237233312925noreply@blogger.com0tag:blogger.com,1999:blog-160318223199333541.post-24759561651972529162010-09-15T19:39:00.009+02:002018-12-16T14:58:35.995+01:00JSF 2.0 - "Hello World" in Eclipse (with *.xhtml pages)<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><b>Note: </b>below description uses <b>Eclipse Indigo</b>, <b>Tomcat 7.0.28</b> and <b>MyFaces 2.1.7</b>. </span><br />
<span style="font-family: inherit;"><b></b><br /></span>
<br />
<div style="font-family: Verdana,sans-serif;">
<b><span style="font-family: inherit;">Requirements:</span></b></div>
<ul style="font-family: Verdana,sans-serif;">
<li><span style="font-family: inherit;">installed Java (description <a href="http://technology-for-human.blogspot.com/2009/04/instalacja-java-jdk-prawie-bezbolesna.html">here</a>)</span></li>
<li><span style="font-family: inherit;">installed and configured Eclipse (description <a href="http://technology-for-human.blogspot.com/2009/04/instalacja-i-konfiguracja-ide-eclipse.html">here</a>)</span></li>
<li><span style="font-family: inherit;">installed and configured Tomcat for the Eclipse (description <a href="http://technology-for-human.blogspot.com/2009/05/konfiguracja-tomcata-w-eclipse.html">here</a>)</span></li>
</ul>
<div style="font-family: Verdana,sans-serif;">
<b><span style="font-family: inherit;">You will learn:</span></b></div>
<ul style="font-family: Verdana,sans-serif;">
<li><span style="font-family: inherit;">how to create "Hello World" application in JSF 2.0 using Eclipse</span></li>
</ul>
<span style="font-family: inherit;"><b>Note 1: </b>The following post and example show classic JSF 2.0 application with a view technology as .xhtml pages. I do not use any additional libraries and extensions for the standard JSF libraries. I don't use (for the time being) templates. JSF implementation used comes from MyFaces.</span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><br /></span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><b>Note 2:</b> I use standard capabilities of IDE platform to generate the project, without those thousands automated plugins which do everything for the user. Sometimes configuration and usage of a plugin is a "pain in the ..." - in my opinion it is worth to know how to deal with a standard set of tools, and after that there is always a time to faciliate our life ;-)</span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><br /></span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;">In this post I will show how to create sample JSF 2.0 application using Eclipse. It will be similar application like in the <a href="http://technology-for-human.blogspot.com/2010/09/jsf-12-hello-world-in-eclipse-with-jsp.html">post</a> about creating sample JSF 1.2 application. But there will be some small differences, especially during generating the project and adding JSF capabilities to it. Let's do it.</span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><br /></span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><span style="font-weight: bold;">Step 1:</span> create new dynamic web project for the Tomcat. It is available in "web" menu section under option "File->New->Project":</span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><br /></span></div>
<div style="font-family: Verdana,sans-serif;">
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh85PuFvndSt_ZMhR8rjtrDsTkdOXJ6jUZUFaM-8SbBq7g3N8qIwei6pHuW5HBHmE7_rneyLCvwpvW5w-hJo24995u5CJSG6qFazIorln7u0NOz66MxrngOLY3Q_OFVCPzh4Jm0iM_bcIwK/s1600/2010.09.15_1.PNG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><span style="font-family: inherit;"><img border="0" height="400" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh85PuFvndSt_ZMhR8rjtrDsTkdOXJ6jUZUFaM-8SbBq7g3N8qIwei6pHuW5HBHmE7_rneyLCvwpvW5w-hJo24995u5CJSG6qFazIorln7u0NOz66MxrngOLY3Q_OFVCPzh4Jm0iM_bcIwK/s400/2010.09.15_1.PNG" width="293" /></span></a></div>
<span style="font-family: inherit;"><br /></span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;">Press "Next" to go further and set some basic project's properties:</span><br />
<span style="font-family: inherit;"><br /></span></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhFuE2EJjzJ2iMYmYvcVeRYN1ZRRkdKxa8_U3Ot8GeixCCO-Q0iCImeRYxsB_dGy5WBZM4r63M3FsAjGr6vaaUb1HkYniCLDUtAKr-g3pj1gc2AohtgSU02qJZnglSVdJFDZ91hLuia7Ld2/s1600/2010.09.15_2.PNG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><span style="font-family: inherit;"><img border="0" height="400" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhFuE2EJjzJ2iMYmYvcVeRYN1ZRRkdKxa8_U3Ot8GeixCCO-Q0iCImeRYxsB_dGy5WBZM4r63M3FsAjGr6vaaUb1HkYniCLDUtAKr-g3pj1gc2AohtgSU02qJZnglSVdJFDZ91hLuia7Ld2/s400/2010.09.15_2.PNG" width="361" /></span></a></div>
<span style="font-family: inherit;"><br /></span>
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiMQjCUB5wEs3lqN6K6hJAHNEGEyoRp1OBsikMuweQLeKch2cv5LsRtsHHvjwBKY-6vRAzJ0IkmhlDBgbeKOAUawmKeSbKZzX3Q8Jjjpd156pSUxYEfdLKbFy6uWwH7k65ZnmQ0WDmHS9m_/s1600/2010.09.15_3.PNG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><span style="font-family: inherit;"><img border="0" height="400" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiMQjCUB5wEs3lqN6K6hJAHNEGEyoRp1OBsikMuweQLeKch2cv5LsRtsHHvjwBKY-6vRAzJ0IkmhlDBgbeKOAUawmKeSbKZzX3Q8Jjjpd156pSUxYEfdLKbFy6uWwH7k65ZnmQ0WDmHS9m_/s400/2010.09.15_3.PNG" width="361" /></span></a></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><br /></span></div>
<div style="font-family: Verdana,sans-serif;">
</div>
<div style="font-family: Verdana,sans-serif;">
</div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><span style="font-weight: bold;">Step 2:</span> adding JSF capabilities for the generated project (JSF and JSTL libraries).</span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><br /></span>
<span style="font-family: inherit;">This step is the same as step 2 in previous <a href="http://technology-for-human.blogspot.com/2010/09/jsf-12-hello-world-in-eclipse-with-jsp.html">JSF 1.2 example under Eclipse</a>. Just copy all necessary .jar libraries into <span style="font-family: "courier new" , "courier" , monospace;">WebContent\WEB-INF\lib</span> directory.</span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><br /></span>
<span style="font-family: inherit;">Also here there is a possibility to use <b>Code Assist</b> for *.xhtml pages. Unfortunately there is one difference comparing to support for JSF 1.2.x - You can click on bean/field <b>only when Your beans are directly declared in <span style="font-family: "courier new" , "courier" , monospace;">faces-config.xml</span> file</b>. If Your beans are declared with the help of annotations, it does not work. This is Eclipse bug (see for example <span style="color: red;">https://bugs.eclipse.org/bugs/show_bug.cgi?id=323709</span>). It is still not fixed in Indigo, despite the fact that it should work in 3.4.x, as noted in the bug.</span><br />
<span style="font-family: inherit;"><br /></span>
<span style="font-family: inherit;">In order to enable <b>Code Assist</b> just open context menu and choose "Properties" option. Locate the section "Project Facets" and check the option "JavaServer Faces". Then "Further configuration available...". On the screen You have to select "Disable library configuration" and press OK: </span><br />
<span style="font-family: inherit;"><br /></span>
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEht-dm_xMHMxG5pgqikL4oHIJJW9LugsA55Q8aDBgul_4Rx1vyCgfxmNh8UmIT_UYGyGrp0xFEMal4k_TcsVuE9EQ_UmCa-5eMmrcKB0fJr7Amt8h59QUmH-VN0MNK_F4pfNHCmID6rJjgD/s1600/2010.09.15_4.PNG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><span style="font-family: inherit;"><img border="0" height="303" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEht-dm_xMHMxG5pgqikL4oHIJJW9LugsA55Q8aDBgul_4Rx1vyCgfxmNh8UmIT_UYGyGrp0xFEMal4k_TcsVuE9EQ_UmCa-5eMmrcKB0fJr7Amt8h59QUmH-VN0MNK_F4pfNHCmID6rJjgD/s400/2010.09.15_4.PNG" width="400" /></span></a></div>
</div>
<div style="font-family: Verdana,sans-serif;">
</div>
<div style="font-family: Verdana,sans-serif;">
</div>
<div class="separator" style="clear: both; text-align: center;">
</div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><br /></span>
<span style="font-family: inherit;"><br /></span>
<br />
<div style="color: black;">
<span style="font-family: inherit;"><b></b></span></div>
</div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><b>Step 3:</b> the project structure is ready. Creating sample application.</span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><br /></span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;">This will be the same application as for version 1.2 with some differences coming from JSF 2.0: </span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><br /></span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;">- pages extenstion will be <span style="font-family: "courier new" , "courier" , monospace;">.xhtml</span> instead of <span style="font-family: "courier new" , "courier" , monospace;">.jsp</span></span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;">- we use annotations in backing beans</span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;">- there will be no <span style="font-family: "courier new" , "courier" , monospace;">faces-config.xml</span> file (now) </span></div>
<div style="font-family: Verdana,sans-serif;">
</div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><br /></span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;">Everything should look like this:</span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><br /></span>
<br />
<div style="color: red;">
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg9MZNiUKK0kosDLsG1RHAK6lWNUHowxXHMZu8seRZP8WlufQKzizUjWCxuqQcoEi8ZwBUaRTyEI-DgfRZ6JX74FfhLRyfUheXZ_WX1AHbx9DHoddvOP_G1aMiCQKpONXb9O43zci7_ID7z/s1600/2010.09.15_5.PNG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><span style="font-family: inherit;"><img border="0" height="237" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg9MZNiUKK0kosDLsG1RHAK6lWNUHowxXHMZu8seRZP8WlufQKzizUjWCxuqQcoEi8ZwBUaRTyEI-DgfRZ6JX74FfhLRyfUheXZ_WX1AHbx9DHoddvOP_G1aMiCQKpONXb9O43zci7_ID7z/s400/2010.09.15_5.PNG" width="400" /></span></a></div>
<span style="font-family: inherit;"><br /></span></div>
</div>
<div class="separator" style="clear: both; text-align: center;">
</div>
<div style="font-family: Verdana,sans-serif;">
</div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;">File <span style="font-family: "courier new" , "courier" , monospace;">web.xml</span> has to be changed like shown on screenshot below:</span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><br /></span>
<br />
<div style="color: red;">
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj9C2BotRbMp8-r8dp5_HCdaKRDt9ku6ZaUjZdG50ZMFm0LONp9FmFIINxEP8ShmS0DjgVDjgIVlsdgzYfqdDdpboR_2Ael-Ow7dLiAZegEZxFm6PIRm-DeaHREnhYaTAgUee-168klqIo3/s1600/2010.09.15_6.PNG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><span style="font-family: inherit;"><img border="0" height="185" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj9C2BotRbMp8-r8dp5_HCdaKRDt9ku6ZaUjZdG50ZMFm0LONp9FmFIINxEP8ShmS0DjgVDjgIVlsdgzYfqdDdpboR_2Ael-Ow7dLiAZegEZxFm6PIRm-DeaHREnhYaTAgUee-168klqIo3/s400/2010.09.15_6.PNG" width="400" /></span></a></div>
<span style="font-family: inherit;"><br /></span></div>
</div>
<div class="separator" style="clear: both; text-align: center;">
</div>
<div class="separator" style="clear: both; text-align: center;">
</div>
<div style="font-family: Verdana,sans-serif;">
</div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><b>Step 4:</b> deployment and running.</span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><br /></span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;">This step is similar to steps 6 and 7 from <a href="http://technology-for-human.blogspot.com/2010/09/jsf-12-hello-world-in-eclipse-with-jsp.html">JSF 1.2 version</a>. After deploying application on the server and starting the server, we have to open a browser and type in URL:</span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><br /></span></div>
<div style="font-family: Verdana,sans-serif;">
<b><span style="font-family: inherit;">http://localhost:8080/FirstJSF2</span></b></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><br /></span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;">That's all. We should have working<b> </b>JSF 2.0 application.</span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><br /></span></div>
<div style="color: red;">
<span style="font-family: inherit;">------------------------------------------- </span></div>
<div style="color: red;">
<span style="font-family: inherit;">Download source files:</span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><br /></span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><b>Note:</b> make sure that <a href="http://technology-for-human.blogspot.com/2009/04/instalacja-java-jdk-prawie-bezbolesna.html">Java</a>, <a href="http://technology-for-human.blogspot.com/2009/04/instalacja-i-konfiguracja-ide-eclipse.html">Eclipse</a> and <a href="http://technology-for-human.blogspot.com/2009/05/konfiguracja-tomcata-w-eclipse.html">Tomcat</a> are properly installed and configured for running the project (additional configuration may be required if different directories are used).</span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><br /></span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;">Eclipse complete sample project is <a href="https://drive.google.com/open?id=1rODg2h5N8hUT2CIvYQa8diKvjsemuybT">here</a> (with all required libraries). You can also download a war file located <a href="https://drive.google.com/open?id=1RJdpnCRxkj4kQdt9sRHwW0X9CymHzoaI">here</a> (just copy it inside <span style="font-family: "courier new" , "courier" , monospace;">webapps</span> folder in Your Tomcat and start Tomcat with the script <span style="font-family: "courier new" , "courier" , monospace;">startup.bat</span>)</span></div>
<div style="font-family: Verdana,sans-serif;">
<br /></div>
Paweł Nieściorukhttp://www.blogger.com/profile/12640446237233312925noreply@blogger.com0tag:blogger.com,1999:blog-160318223199333541.post-23321812530081700982010-09-01T20:45:00.018+02:002018-12-16T14:43:33.686+01:00JSF 1.2 - "Hello World" in Eclipse (with *.jsp pages)<div style="font-family: Verdana,sans-serif;">
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><b>Note: </b>below description uses <b>Eclipse Indigo</b>, <b>Tomcat 7.0.28</b>, <b>MyFaces 1.2.12</b> and <b>JSTL 1.2.1</b>.<b> </b></span><br />
<span style="font-family: inherit;"><br /></span>
<b><span style="font-family: inherit;">Requirements:</span></b></div>
<ul style="font-family: Verdana,sans-serif;">
<li><span style="font-family: inherit;">installed Java (description <a href="http://technology-for-human.blogspot.com/2009/04/instalacja-java-jdk-prawie-bezbolesna.html">here</a>)</span></li>
<li><span style="font-family: inherit;">installed and configured Eclipse (description <a href="http://technology-for-human.blogspot.com/2009/04/instalacja-i-konfiguracja-ide-eclipse.html">here</a>)</span></li>
<li><span style="font-family: inherit;">installed and configured Tomcat for the Eclipse (description <a href="http://technology-for-human.blogspot.com/2009/05/konfiguracja-tomcata-w-eclipse.html">here</a>)</span></li>
</ul>
<div style="font-family: Verdana,sans-serif;">
<b><span style="font-family: inherit;">You will learn:</span></b></div>
<ul style="font-family: Verdana,sans-serif;">
<li><span style="font-family: inherit;">how to create "Hello World" application in JSF 1.2 using Eclipse</span></li>
</ul>
<span style="font-family: inherit;"><b>Note 1: </b>The following post and example show classic JSF 1.2 application with a view technology as .jsp pages. I do not use any additional libraries and extensions for the standard JSF libraries.</span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><br /></span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><b>Note 2:</b> I use standard capabilities of IDE platform to generate the project, without those thousands automated plugins which do everything for the user. Sometimes configuration and usage of a plugin is a "pain in the ..." - in my opinion it is worth to know how to deal with a standard set of tools, and after that there is always a time to faciliate our life ;-)</span></div>
<div style="font-family: Verdana,sans-serif;">
</div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;">Many publications and tutorials about JSF (Java Server Faces) very briefly descibe the configuration of development environment for the described examples. Many times I saw such situation: after few sentences about "how amazing is JSF" there was a simple list of JSF tags with their attributes described. Or at the beginning there was an example of "Hello World" application and every line of code was described and explained.</span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;">I think that is what is missing is the lack of an example of complete project with its directory structure and an information how to create this project and deploy on the web server. I saw some examples where the project structure was shown, but for the building and deploying application user had to use Ant and type some commands. Great, but we need Ant, when we have Ant, we need to be familiar with it to write build.xml file, then we need something else and so on and so on.</span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;">This is of course OK if we want to write everything from scratch by hand. But wouldn't it better just to start our IDE, make some clicks and have a complete and working project ready for further development?</span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;">Let's see how it looks like in Eclipse IDE. </span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><br /></span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><b>Step 1:</b> creating dynamic web project for the Tomcat.</span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><br /></span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;">Open menu "File->New->Project" and in the "web" section find and choose "Dynamic Web Project" and press "Next":</span><br />
<div class="separator" style="clear: both; text-align: left;">
<span style="font-family: inherit;"> </span></div>
</div>
<div style="font-family: Verdana,sans-serif;">
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgfBI8BXiZUSMF-uDEUF4YB_2RTHSdFHWrlVa-TsIiZzQBetRT9lJmE4DObASUP_V56rn1aIpYNTKTSkjry3eKZxotxhfXJGAZDdKynUYwTYheOphR1VnKP0q8OvRnsBbWN7xP1eCCUNP40/s1600/2010.09.01_01.PNG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><span style="font-family: inherit;"><img border="0" height="400" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgfBI8BXiZUSMF-uDEUF4YB_2RTHSdFHWrlVa-TsIiZzQBetRT9lJmE4DObASUP_V56rn1aIpYNTKTSkjry3eKZxotxhfXJGAZDdKynUYwTYheOphR1VnKP0q8OvRnsBbWN7xP1eCCUNP40/s400/2010.09.01_01.PNG" width="341" /></span></a></div>
<div class="separator" style="clear: both; text-align: left;">
</div>
<span style="font-family: inherit;"><br /></span></div>
<div style="font-family: Verdana,sans-serif;">
</div>
<div style="font-family: Verdana,sans-serif;">
</div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;">Press "Next" to go further and set some basic project's properties:</span></div>
<div style="font-family: Verdana,sans-serif;">
</div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgwUOg9iSbUKEeSKiqnKKzym8PfwWW1h1a5IwOsvbhIIN7UygFa5yxTIaCerF9_TfX5KBD4Kc-Pyg51Stshb8PkCQ7ML8Kua5zhJChQ5ZjxLEmGgyhFLgF0bi6QRLjDouPE9VAoDAV-ngRR/s1600/2010.09.01_02.PNG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><span style="font-family: inherit;"><img border="0" height="400" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgwUOg9iSbUKEeSKiqnKKzym8PfwWW1h1a5IwOsvbhIIN7UygFa5yxTIaCerF9_TfX5KBD4Kc-Pyg51Stshb8PkCQ7ML8Kua5zhJChQ5ZjxLEmGgyhFLgF0bi6QRLjDouPE9VAoDAV-ngRR/s400/2010.09.01_02.PNG" width="361" /></span></a></div>
<span style="font-family: inherit;"><br /></span>
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiyqkyisPg9dbvI2nvVWbmcloPpnSlZ2EiWJXXhZZUP8kjUw93ZvABGF7gqNuVKLlDpHCNYuHCUq0_UKfCOvAzNl4ZThPCkMJlAV_mgFH_sKB87oZ6oi_-dMP_DLuSIrv4zpR3_T_Ov0Z3d/s1600/2010.09.01_03.PNG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><span style="font-family: inherit;"><img border="0" height="400" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiyqkyisPg9dbvI2nvVWbmcloPpnSlZ2EiWJXXhZZUP8kjUw93ZvABGF7gqNuVKLlDpHCNYuHCUq0_UKfCOvAzNl4ZThPCkMJlAV_mgFH_sKB87oZ6oi_-dMP_DLuSIrv4zpR3_T_Ov0Z3d/s400/2010.09.01_03.PNG" width="361" /></span></a></div>
<div style="font-family: Verdana,sans-serif;">
</div>
<div style="font-family: Verdana,sans-serif;">
</div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;">After that we should have generated project structure for the web application.</span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><span style="font-weight: bold;"><br />
Step 2:</span> adding JSF capabilities for the generated project (JSF and JSTL libraries).</span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><br /></span>
<span style="font-family: inherit;">The easiest way is to download latest libraries of MyFaces 1.2.x from <a href="http://myfaces.apache.org/download.html">here</a> and JSTL 1.2.x from <a href="http://jstl.java.net/download.html">here</a>, and copy all .jar files into generated <span style="font-family: "courier new" , "courier" , monospace;">WebContent\WEB-INF\lib</span> directory. After that just
refresh the whole project by pressing "F5" on the project's root and all
libraries should appear in the section named "Web App Libraries" in the
project's structure.</span><br />
<span style="font-family: inherit;"><br /></span>
<span style="font-family: inherit;">In addition it is worth to enable nice feature named <b>Code Assist</b>. Code Assist allows automatically complete JSF tags when typying by
pressing CTRL + Space. It also provides direct access to beans, their
methods or fields directly from JSP page - just by click on bean/field
name. In order to do this just right click on the project and from the opened context menu choose "Properties" option. Locate the section "Project Facets" and check the option "JavaServer Faces". Then "Further configuration available...". On the
screen You have to select "Disable library configuration" and press OK:</span><br />
<span style="font-family: inherit;"><br /></span>
<br />
<div style="color: red;">
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjDWuftJjh_VlhdrOL0c7g21icxKLmR9Vo2p16N6-P2f-9Mb_IuHiwCVoDLqwqzUAZgS-QsOWKyP33p00iZGjQRwO9uiTDYThBS73lh8NYdmdcqfnRLq13UJiU_L70YdGFvev7dhIm-gX8D/s1600/2010.09.01_04.PNG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><span style="font-family: inherit;"><img border="0" height="293" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjDWuftJjh_VlhdrOL0c7g21icxKLmR9Vo2p16N6-P2f-9Mb_IuHiwCVoDLqwqzUAZgS-QsOWKyP33p00iZGjQRwO9uiTDYThBS73lh8NYdmdcqfnRLq13UJiU_L70YdGFvev7dhIm-gX8D/s400/2010.09.01_04.PNG" width="400" /></span></a></div>
<span style="font-family: inherit;"><br /></span></div>
</div>
<div style="font-family: Verdana,sans-serif;">
<div class="separator" style="clear: both; text-align: center;">
</div>
</div>
<div style="font-family: Verdana,sans-serif;">
</div>
<span style="font-family: inherit;"><br /></span>
<br />
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><b>Note:</b> project facet "JavaServer Faces" allows to download JSF libraries automatically and add them to the project. Unfortunately JSTL libraries still needs to be downloaded manually.</span></div>
<div style="font-family: Verdana,sans-serif;">
</div>
<span style="font-family: inherit;"><br /></span>
<br />
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><span style="font-weight: bold;">Step</span><span style="font-weight: bold;"> 3:</span> the overview of a project.</span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><br /></span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;">When steps 1-2 where done as described, the project structure should look like this:</span><br />
<span style="font-family: inherit;"><br /></span>
<br />
<div style="color: red;">
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgUIHHFVWXjGtUvRB0BK1x8nieATVKEuIqf1TOHrPHH717LZ5FZFyZCDSQCW_b8w7qYjsRTHFklu64OUaY2U7U10ZrqxjQm365tUT-2BP_HkXXDeLBFDiSWDrGuWjHKTOu3V_tBUVftZN43/s1600/2010.09.01_05.PNG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><span style="font-family: inherit;"><img border="0" height="240" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgUIHHFVWXjGtUvRB0BK1x8nieATVKEuIqf1TOHrPHH717LZ5FZFyZCDSQCW_b8w7qYjsRTHFklu64OUaY2U7U10ZrqxjQm365tUT-2BP_HkXXDeLBFDiSWDrGuWjHKTOu3V_tBUVftZN43/s400/2010.09.01_05.PNG" width="400" /></span></a></div>
<span style="font-family: inherit;"><br /></span></div>
</div>
<div style="font-family: Verdana,sans-serif;">
<div class="separator" style="clear: both; text-align: center;">
</div>
</div>
<div style="font-family: Verdana,sans-serif;">
</div>
<div style="font-family: Verdana,sans-serif;">
</div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><span style="font-weight: bold;">Step 4:</span> "Hello World" application.</span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><br /></span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;">Now we are ready to implement some sample JSF application. First we should create a package named <span style="font-family: "courier new" , "courier" , monospace;">com.firstjsf.backingbeans</span> in the "src" directory. Second we should create two classes inside the package named <span style="font-family: "courier new" , "courier" , monospace;">HelloMessageBean</span> and <span style="font-family: "courier new" , "courier" , monospace;">WelcomeBean</span>. Third we should modify generated <span style="font-family: "courier new" , "courier" , monospace;">faces-config.xml</span> file by adding there our classes and navigation rules. Everything should look like this:</span><br />
<span style="font-family: inherit;"><br /></span>
<br />
<div style="color: red;">
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj7zRljD5vcutWPqfSRsOVfYFVDfZNov1eW99bWLNpxzQmDyDkh4T7kWVHqNHEO-UOncE4PMZyVGvwtsnkhcWmeCoi3B5b00sbGsHQ-QnpIYAHgepPzcnGcZYWUR5NluUhwOGvsICNSgDDo/s1600/2010.09.01_06.PNG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><span style="font-family: inherit;"><img border="0" height="211" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj7zRljD5vcutWPqfSRsOVfYFVDfZNov1eW99bWLNpxzQmDyDkh4T7kWVHqNHEO-UOncE4PMZyVGvwtsnkhcWmeCoi3B5b00sbGsHQ-QnpIYAHgepPzcnGcZYWUR5NluUhwOGvsICNSgDDo/s400/2010.09.01_06.PNG" width="400" /></span></a></div>
<span style="font-family: inherit;"><br /></span></div>
</div>
<div style="font-family: Verdana,sans-serif;">
<div class="separator" style="clear: both; text-align: center;">
</div>
</div>
<div style="font-family: Verdana,sans-serif;">
</div>
<div style="font-family: Verdana,sans-serif;">
</div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;">Be careful about small and big letters - names should be exactly as shown above. In the <span style="font-family: "courier new" , "courier" , monospace;">faces-config.xml</span> file we created entries for the navigation rules for not existing yet web pages. Let's create those web pages (<span style="font-family: "courier new" , "courier" , monospace;">index.jsp</span> and <span style="font-family: "courier new" , "courier" , monospace;">message.jsp</span>) in the <span style="font-family: "courier new" , "courier" , monospace;">WebContent</span><span style="font-style: italic;"> </span>directory:</span><br />
<span style="font-family: inherit;"><br /></span>
<br />
<div style="color: red;">
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg-JPyDHdBot7dvy38f1tOrWUNB7tBDJTjl5qOiIxElRzGa4kJySrzqKZYcRMrDuy9WkMXW0M-9LIy2UQvTKCfu2_ze3BIa_safvJnYURalxOyKllYBSfUL5b-gzBM1-9iESxz6cbYMhp-9/s1600/2010.09.01_07.PNG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><span style="font-family: inherit;"><img border="0" height="303" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg-JPyDHdBot7dvy38f1tOrWUNB7tBDJTjl5qOiIxElRzGa4kJySrzqKZYcRMrDuy9WkMXW0M-9LIy2UQvTKCfu2_ze3BIa_safvJnYURalxOyKllYBSfUL5b-gzBM1-9iESxz6cbYMhp-9/s400/2010.09.01_07.PNG" width="400" /></span></a></div>
<span style="font-family: inherit;"><br /></span></div>
</div>
<div style="font-family: Verdana,sans-serif;">
<div class="separator" style="clear: both; text-align: center;">
</div>
</div>
<div style="font-family: Verdana,sans-serif;">
</div>
<div style="font-family: Verdana,sans-serif;">
</div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit; font-weight: bold;">Application is ready.</span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><br /></span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><span style="font-weight: bold;">Step 5:</span> deploying on Tomcat. </span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><br /></span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;">At the bottom of a screen locate "Severs" tab. We should have already Tomcat there. Right click on server name to open its context menu, ten choose option "Add and Remove...":</span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><br /></span>
<br />
<div style="color: red;">
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgisB4YEbKuu98dEFIy8CLPqBMxPS-HuRdczb2Frfs-sXZclZJjNL3Ni533s2aUKfsgYajMs7agtpBIeineI0DCR-w_du6SeJQgIQIM2AIGrSRuWyMXewHiRpDNSYKzE1PJLhG7UlhrgCbR/s1600/2010.09.01_08.PNG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><span style="font-family: inherit;"><img border="0" height="242" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgisB4YEbKuu98dEFIy8CLPqBMxPS-HuRdczb2Frfs-sXZclZJjNL3Ni533s2aUKfsgYajMs7agtpBIeineI0DCR-w_du6SeJQgIQIM2AIGrSRuWyMXewHiRpDNSYKzE1PJLhG7UlhrgCbR/s400/2010.09.01_08.PNG" width="400" /></span></a></div>
<span style="font-family: inherit;"><br /></span></div>
</div>
<div style="font-family: Verdana,sans-serif;">
</div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;">Choose our project and add it to the server by pressing "Add" button:</span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><br /></span>
<br />
<div style="color: red;">
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgmLyUBRqtY0_J9_Mvks8TVg6SwACXlErzx7GnuKcyRiqgaf24m5cNVWIz-NuaHYe24HekAfUc1ZcwNpKjIXkUMWhnkcoCPk9WKv8Vlohndaim_JzTlafpiAsAkoaUDc1sGBzvaiNlq8-Wl/s1600/2010.09.01_09.PNG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><span style="font-family: inherit;"><img border="0" height="400" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgmLyUBRqtY0_J9_Mvks8TVg6SwACXlErzx7GnuKcyRiqgaf24m5cNVWIz-NuaHYe24HekAfUc1ZcwNpKjIXkUMWhnkcoCPk9WKv8Vlohndaim_JzTlafpiAsAkoaUDc1sGBzvaiNlq8-Wl/s400/2010.09.01_09.PNG" width="391" /></span></a></div>
<span style="font-family: inherit;"><br /></span></div>
</div>
<div style="font-family: Verdana,sans-serif;">
</div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><span style="font-weight: bold;">Step 6:</span> running the application.</span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><br /></span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;">Go back to the "Server" tab again and make sure that our project is visible on the server. Then click the marked icon to start the server:</span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><br /></span>
<br />
<div style="color: red;">
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh_-NpkCLVWZ05Rk7s3lHj-s3WcE1R0c-tg-EhaR6aECa72Z_sru5lM69IZec49JD4uKOcSxqtYqOkIAtvrLjX8sK7wJlxpTKKGIqI3mARmARHtrH6Nv2WrPzaBy97Tnbaie_uLP4bzhjMM/s1600/2010.09.01_10.PNG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><span style="font-family: inherit;"><img border="0" height="115" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh_-NpkCLVWZ05Rk7s3lHj-s3WcE1R0c-tg-EhaR6aECa72Z_sru5lM69IZec49JD4uKOcSxqtYqOkIAtvrLjX8sK7wJlxpTKKGIqI3mARmARHtrH6Nv2WrPzaBy97Tnbaie_uLP4bzhjMM/s400/2010.09.01_10.PNG" width="400" /></span></a></div>
<span style="font-family: inherit;"><br /></span></div>
</div>
<div style="font-family: Verdana,sans-serif;">
</div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;">After starting the server we should see that the server state was changed:</span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><br /></span></div>
<div style="font-family: Verdana,sans-serif;">
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjc1dFgRwCAgrxZOb1E73s7q9gWrU4UWmX9m2wW38-MEOJItEb5OwWqdVb5NUsbg2jUnuLJPoCwWJWTyZ554d-LqCvqyM75ZG1-XqMTYNRhBrUodH0J3dfzMdkvEQC3WUhPwB4QSrjookme/s1600/2010.09.01_11.PNG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><span style="font-family: inherit;"><img border="0" height="111" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjc1dFgRwCAgrxZOb1E73s7q9gWrU4UWmX9m2wW38-MEOJItEb5OwWqdVb5NUsbg2jUnuLJPoCwWJWTyZ554d-LqCvqyM75ZG1-XqMTYNRhBrUodH0J3dfzMdkvEQC3WUhPwB4QSrjookme/s400/2010.09.01_11.PNG" width="400" /></span></a></div>
<span style="font-family: inherit;"><br /></span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;">After deploying application on the server and starting the server, we have to open a browser and type in URL:</span></div>
<div style="font-family: Verdana,sans-serif;">
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><br /></span></div>
<div style="font-family: Verdana,sans-serif;">
<b><span style="font-family: inherit;">http://localhost:8080/FirstJSF</span></b></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><br /></span></div>
</div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><span style="font-weight: bold;">What next?</span> This simple working project can be used as a base for further learning about JSF. For example we can change the view technology from .jsp to facelets. We can add libraries which extend our standard tags by adding tags with ajax support, i.e <a href="http://livedemo.exadel.com/richfaces-demo/index.jsp">RichFaces</a>.</span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><br /></span></div>
<div style="color: red;">
<span style="font-family: inherit;">------------------------------------------- </span></div>
<div style="color: red;">
<span style="font-family: inherit;">Download source files:</span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><br /></span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><b>Note:</b> make sure that <a href="http://technology-for-human.blogspot.com/2009/04/instalacja-java-jdk-prawie-bezbolesna.html">Java</a>, <a href="http://technology-for-human.blogspot.com/2009/04/instalacja-i-konfiguracja-ide-eclipse.html">Eclipse</a> and <a href="http://technology-for-human.blogspot.com/2009/05/konfiguracja-tomcata-w-eclipse.html">Tomcat</a> are properly installed and configured for running the project (additional configuration may be required if different directories are used).</span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><br /></span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;">Eclipse complete sample project is <a href="https://drive.google.com/file/d/1oWilBPKEXWG6oitVlrpaO8rhfGP5vbTe/view?usp=sharing">here</a>. External libraries folder (with JSF and JSTL) is <a href="https://drive.google.com/file/d/1oKQd2BEqICymkNaJz_bd2bWi7xAXtk0n/view?usp=sharing">here</a>. You can also download a war file located <a href="https://drive.google.com/open?id=1ygR7IrrbhiJWp-T5Ki8Bdgag4qT-HG_s">here</a> (just copy it inside <span style="font-family: "courier new" , "courier" , monospace;">webapps</span> folder in Your Tomcat and start Tomcat with the script <span style="font-family: "courier new" , "courier" , monospace;">startup.bat</span>)</span></div>
Paweł Nieściorukhttp://www.blogger.com/profile/12640446237233312925noreply@blogger.com4tag:blogger.com,1999:blog-160318223199333541.post-74377949854175607702010-02-24T23:50:00.267+01:002018-12-16T13:19:47.929+01:00"My databse is slow" - part 3: some technical stuff<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><b>Note: </b>below description uses <b>PostgreSQL 8.3.7.</b></span><br />
<span style="font-family: inherit;"><span style="font-family: inherit;"><br /></span>
<span style="font-family: inherit;">In the previous two articles I tried to show what can affect the overall <a href="http://technology-for-human.blogspot.com/2009/10/baza-muli-czesc-1.html">performance</a> of the database and how important are <a href="http://technology-for-human.blogspot.com/2010/01/baza-muli-czesc-2.html">indexes</a>.</span></span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;">If we assume that the database is normalized, memory settings for the server are ok and we have indexes, the only way to check the performance is monitoring the query plans. Therefore I would like to return once again to the query plans and look at them with more details.</span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><br /></span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;">From the previous post, we know that we can generate plans for queries using two commands. These commands are:</span></div>
<ul style="font-family: Verdana,sans-serif;">
<li><span style="font-family: inherit;"><b></b>EXPLAIN SQL_command</span></li>
<li><span style="font-family: inherit;">EXPLAIN ANALYZE SQL_command - this command is a variant of the first with small difference: examined query is executed and EXPLAIN ANALYZE returns the real execuion time.</span></li>
</ul>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;">Using the test database from the previous post, let's see the query plan for the following SQL command: </span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><br /></span></div>
<pre class="prettyprint" style="font-family: Verdana,sans-serif;">select * from customers where lname = 'Cervantes';</pre>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><br /></span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;">To see the query plan, execute those two commands:</span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><br /></span></div>
<pre class="prettyprint" style="font-family: Verdana,sans-serif;">vacuum analyze;
explain select * from customers where lname = 'Cervantes';</pre>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><br /></span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;">Query plan that has been generated for this query is as follows: </span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><br /></span></div>
<div style="font-family: Verdana,sans-serif;">
</div>
<div style="font-family: Verdana,sans-serif;">
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgfSJcxOE5zgWRxUVAASJ4uUTl1g-4bV0iFTQGgdFn-KS2E6qW60R5M3p44EDfak0u3rfRBeNBZrBr-nAPrM5al8rxar0R-ouaBExW5HzPf1uhs2_b9t_t6dvO5CEemrQVTtOtJsv_ervVz/s1600/2010.02.24_01.PNG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><span style="font-family: inherit;"><img border="0" height="136" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgfSJcxOE5zgWRxUVAASJ4uUTl1g-4bV0iFTQGgdFn-KS2E6qW60R5M3p44EDfak0u3rfRBeNBZrBr-nAPrM5al8rxar0R-ouaBExW5HzPf1uhs2_b9t_t6dvO5CEemrQVTtOtJsv_ervVz/s400/2010.02.24_01.PNG" width="400" /></span></a></div>
<span style="font-family: inherit;"><br /></span></div>
<div style="font-family: Verdana,sans-serif;">
</div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;">Always we are most interested in <b>"the highest" line of displayed plan</b> - it shows the <b>total cost of the query execution</b>, after all other internal operations for more complex queries (eg joins, sorts, etc.).</span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><br /></span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;">In our case we have: (cost: 4.51 .. 59.47 rows = 33 width = 18). What does it mean?</span></div>
<ol style="font-family: Verdana,sans-serif;">
<li><span style="font-family: inherit;"><b>cost: 4.51 .. 59.47</b> - this is an interval defining total execution time measured in special units called disk page fetches - these units are not seconds nor other units of time. First number estimates time needed to retrieve first row in the result, second estimates total cost of returning all rows in the result. The calculation of this value is "higher mathematics", especially if the query uses more techniques than primitive table sequential scanning - which is the easiest and can be easily calculated. Unfortunately, but only for primitive examples like "select * from some_table" (without the use of indices to be 100% sure that the scan will be performed sequentially and without any query conditions "where"), You can find over the Web some examples of calculating these values. Those who are interested in more details and technical stuff, should visit <a href="http://doxygen.postgresql.org/costsize_8c.html">this page</a></span></li>
<li><span style="font-family: inherit;"><b>rows = 33</b> - this is estimated number of rows returned by a query.</span></li>
<li><span style="font-family: inherit;"><b>width = 18</b> - this is estimated size (in bytes) of all the returned rows</span></li>
</ol>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><br /></span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;">Above data are obtained on the basis of statistics located in a special system tables. There are two tables of statistics which are crucial for the query planner:</span></div>
<ol style="font-family: Verdana,sans-serif;">
<li><span style="font-family: inherit;"><b>pg_class </b>- contains information about certain database relations (tables, indexes, etc.), i.e. their location and size.</span></li>
<li><span style="font-family: inherit;"><b>pg_stats</b> - contains information about the data stored in tables, i.e. the most common values, the frequency of common values occurrence.</span></li>
</ol>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;">Let's see what are the statistics for the table "customers", and what is their impact on some elements of the query plan for above query about user "Cervantes".</span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><span style="font-family: inherit;"><br /></span>
</span><br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgJKE0uMcU8fVvgG1uU0SIC_1J99KOfl_Oa7SaWnV2-uZN8eDlnfxBV76aG4ACfDMvaFfHy-9vI-gK3pj0mQOw4jywIbHe2pgoCQ545fczmENu0iAYoabY11zYUBR393H9q6HCK0O-P_msT/s1600/2010.02.24_02.PNG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><span style="font-family: inherit;"><img border="0" height="131" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgJKE0uMcU8fVvgG1uU0SIC_1J99KOfl_Oa7SaWnV2-uZN8eDlnfxBV76aG4ACfDMvaFfHy-9vI-gK3pj0mQOw4jywIbHe2pgoCQ545fczmENu0iAYoabY11zYUBR393H9q6HCK0O-P_msT/s400/2010.02.24_02.PNG" width="400" /></span></a></div>
<span style="font-family: inherit;"><span style="font-family: inherit;"><br /></span>
<span style="font-family: inherit;">Returned data mean that the table "cutomers" has 10.000 records and is located on 62 "pages".</span></span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><br /></span></div>
<div style="font-family: Verdana,sans-serif;">
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgk050rT_RGTIumXtw6kqUvzEgk9zoBHcLAJVv5MMughV7ipF3M1566l0Wg0RS5Lo1YHZevjuDxzUEJWlduiomoR8N33mOxveK1ga0pBDIlLC_ZCaYx_b1yHfVRgzec7Yy9dtsGOOOiRnN5/s1600/2010.02.24_03.PNG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><span style="font-family: inherit;"><img border="0" height="103" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgk050rT_RGTIumXtw6kqUvzEgk9zoBHcLAJVv5MMughV7ipF3M1566l0Wg0RS5Lo1YHZevjuDxzUEJWlduiomoR8N33mOxveK1ga0pBDIlLC_ZCaYx_b1yHfVRgzec7Yy9dtsGOOOiRnN5/s400/2010.02.24_03.PNG" width="400" /></span></a></div>
<span style="font-family: inherit;"><br /></span></div>
<div style="font-family: Verdana,sans-serif;">
</div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;">Returned data are based on some statistical data for all the columns in the table "cusotmers" and their content. It is specified which values are most popular, what is their frequency, and amount of values which are different from each other.</span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;">What is the meaning of this data in the context of our query about "Cervantes" user? </span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><br /></span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><b>pg_class</b> - is the basis in calculating the cost of the query execution. For our "Cervantes" query in the case of sequential scan (if there were no index on the "lname" field in the Customers table), we would calculate the cost from the formula:</span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit; font-style: italic;">(relpages * SEQ_PAGE_COST) + (reltuples * CPU_TUPLE_COST) + (relpages * CPU_OPERATOR_COST)</span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;">Capitalized names are costs of arbitrary operation - the are defined in postgresql.conf. Unfortunately (or "fortunately"), our query uses indexes, so the calculation of the cost is a bit more complicated (task for volunteers, formulas are <a href="http://doxygen.postgresql.org/costsize_8c.html">here</a>, You only have to to be familiar with the concepts of "last recently used buffers" (LRU) and Mackert-Lohman formula ;-)).</span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><br /></span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><b>pg_stats</b> - allows to estimate the number of rows returned in a query using given logical condition (in our case: "where lname = 'Cervantes'"). In our case for the "Cervantes" query we are lucky - "Cervantes" occurs directly in the list of most common surnames, and also is known for its occurrence. The number of rows returned is calculated using the formula:</span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit; font-style: italic;">relpages * selectivity</span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;">where:</span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit; font-style: italic;">selectivity = most_common_freqs["Cervantes"]</span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;">Hence we get:</span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;">10000 * 0,00333333 = 33</span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;">And what if we are looking for value which is not in the column "most_common_freqs"? Then the formula has a different form:</span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit; font-style: italic;">selectivity = (1 - sum(most_common_freqs))/(num_distinct - num_most_common_vals)</span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;">Calculating selectivity value for the name "Smith", I leave for volunteers as usually ;-). Remember that first You have to execute "vacuum analyze" and then view statistics and then the query plan.</span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><br /></span></div>
<div style="color: red;">
<span style="font-family: inherit;">-------------------------------------------- </span></div>
<div style="color: red;">
<span style="font-family: inherit;">Some notes about the statistics:</span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><br /></span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;">The content of statistics tables is generated based on<b> random samples taken from the tables in the database</b>. Generating (and therefore refreshing) full statistics takes place during the execution of the <b>VACUUM ANALYZE</b> command. Hence, it is very important to <b>run this command periodically</b> to ensure that statistics for the planner are up to date. Therefore, before showing the above example the first command executed by me was VACUUM ANALYZE in order to query plan be based on the most reliable statistics.</span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;">It should be noted that next execution of this command can generate a slightly different statistics, so query plan for the same query may be slightly different (in terms of cost and estimated number of rows). This is because of mentioned random samples taken for calculations.</span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><br /></span></div>
<div style="color: red;">
<span style="font-family: inherit;">--------------------------------------------</span></div>
<div style="color: red;">
<span style="font-family: inherit;">A few final notes about performance</span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><br /></span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;">I can say that the general rule to optimize query execution time is as follows:</span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;">"There is no certain way to increase performance in all cases. The best method is trial and error."</span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;">In practice it means to try to <b>generate different query plans</b> (in addition to that which is generated by default) and select those with the best execution time. These attempts usually involve forcing the query planner to behave in different way which leads to a different query plan. This can be achieved through:</span><br />
<span style="font-family: inherit;"><span style="font-family: inherit;"><br /></span>
<span style="font-family: inherit;"><b>1.</b> Forcing the query planner to use an index (often the index exists but is not used by the planner). This can be done for example by:</span></span><br />
<ul>
<li><span style="font-family: inherit;">switching off a global sequential scans by setting enable_seqscan = off in postgresql.conf file</span></li>
<li><span style="font-family: inherit;">partial indexes (conditional) - You can create a conditional index for the most frequently returned data in column</span></li>
<li><span style="font-family: inherit;">indexes using multiple fields (watch out for the order - the order of fields in the index should be the same as the order of fields in queries)</span></li>
<li><span style="font-family: inherit;">some SQL tricks related to change the query selectivity, for example using LIMIT, ORDER BY or trailing condition AND TABLE_ID = TABLE_ID (where TABLE_ID is a master key in the table)</span></li>
</ul>
</div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><b>2. </b>Changing the order of joining multiple tables, for example executing "(A join B) join C" can be different than executing "A join (B join C)" - this is similar to a problem of "optmial matrix-chain multiplication".</span><br />
<span style="font-family: inherit;"><b>3.</b> Using stored procedures, views, breaking huge query into a number of smaller queries .</span><br />
<b><span style="font-family: inherit;"> </span></b><br />
<b><span style="font-family: inherit;">Interesting fact (found over the Web): </span></b></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;">In order to monitor the progress of executing a long query to the database (a really long time, not a few seconds) you can create a sequence with the value set to 1 (eg, the sequence "monitor_seq") and add it to executing query:</span></div>
<span style="font-family: inherit;"><span style="font-family: inherit;"><br /></span>
</span><br />
<pre class="prettyprint" style="font-family: Verdana,sans-serif;">select nextval('monitor_seq'), column1, column2....</pre>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><br /></span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;">Executing:</span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><br /></span></div>
<pre class="prettyprint" style="font-family: Verdana,sans-serif;">select currval('monitor_seq');</pre>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><br /></span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;">at the time of running the long query will show us the progress.</span></div>
Paweł Nieściorukhttp://www.blogger.com/profile/12640446237233312925noreply@blogger.com0tag:blogger.com,1999:blog-160318223199333541.post-51270905174476905452010-01-05T21:25:00.124+01:002018-12-16T13:14:39.488+01:00"My database is slow" - part 2: indexes.<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><b>Note: </b>below description uses <b>PostgreSQL 8.3.7.</b> </span><br />
<span style="font-family: inherit;"><br /></span>
<span style="font-family: inherit;">This article is a continuation of <a href="http://technology-for-human.blogspot.com/2009/10/baza-muli-czesc-1.html">previous one</a> about database performance.</span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;">Suppose we have a database that is normalized to our needs, the memory of the server was also quite reasonably allocated, we are using a connection pool and we did not change the default transaction isolation level for our database - but the database still is not a "speed daemon". What can You do in this situation? Did we miss something?</span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><br /></span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;">You can look inside the database giva a closer look on specific operations performed on the data to determine which operations cause performance degradation and possibly "help" them perform faster. As the sample database I will use PostgreSQL - I will examine its specific mechanisms to check and improve performance. This will be only an outline of what can be done (based on crude example), because the topic is so large that it may well be placed on a separate dedicated blog.</span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><br /></span></div>
<div style="font-family: Verdana,sans-serif;">
<b><span style="font-family: inherit;">Requirements:</span></b><br />
<ul>
<li><span style="font-family: inherit;">installed PostgreSQL (description <a href="http://technology-for-human.blogspot.com/2009/04/instalacja-postgresql-wersja-soft.html">here</a>)</span></li>
<li><span style="font-family: inherit;">a test database with <b>sample data</b> - just to show how PostgreSQL performs a very simple SQL opeartions </span></li>
</ul>
</div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><b></b>Let's consider a very simple database for some online store with two tables: customer table (called Customer) and customers' orders table (called Order). Just open pgAdmin and execute this SQL command:</span></div>
<span style="font-family: inherit;"><br /></span>
<br />
<pre class="prettyprint" style="font-family: Verdana,sans-serif;">CREATE DATABASE store ENCODING = 'UTF8';</pre>
<span style="font-family: inherit;"><br /></span>
<br />
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;">Then connect to database "store" and run this SQL commands:</span></div>
<span style="font-family: inherit;"><br /></span>
<br />
<pre class="prettyprint" style="font-family: Verdana,sans-serif;">CREATE TABLE customers
(
customer_id INTEGER,
fname VARCHAR(32),
lname VARCHAR(32) NOT NULL,
CONSTRAINT customer_pk PRIMARY KEY(customer_id)
);
CREATE TABLE orders
(
order_id INTEGER,
customer_id INTEGRE NOT NULL CONSTRAINT customer_order_fk REFERENCES customers(customer_id),
description TEXT,
price NUMERIC(7,2),
CONSTRAINT order_pk PRIMARY KEY(order_id)
);
</pre>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><br /></span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;">For simplicity, here I do not use a SERIAL field to hold primary keys - just ordinary integer with constraint. Now we should fill the database with some <b>sample data</b>: lets put about 10 000 records to Customers table and 30 000 records to Orders table. That is rather huge amount of data. How can we do it?</span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;">The best way is to write a Java program that generates a file with proper SQL INSERT statements for each table - this is homework for You ;-)</span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;">I wrote simple program which takes <a href="http://names.mongabay.com/male_names.htm">popular first names for males</a> and <a href="http://names.mongabay.com/female_names.htm">popular first names for females</a>, then takes popular <a href="http://names.mongabay.com/data/1000.html">surnames</a> from here and combines them together to produce 10.000 "people". Having those records and knowing their customer_id (that's why I used ordinary integer field - just to write a program) my program generated random 30 000 orders for those customers.</span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><br /></span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;">And now suppose we want to <b>retrieve all customers named Smith</b> and we want to <b>see all orders for each customer Smith</b>. We need <b>two queries</b>:</span><br />
<span style="font-family: inherit;"><br /></span>
<br />
<pre class="prettyprint" style="font-family: Verdana,sans-serif;">select * from customers where lname = 'Smith';
select * from orders join customers on orders.customer_id=customers.customer_id where customers.lname = 'Smith';</pre>
</div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><br /></span></div>
<div style="font-family: Verdana,sans-serif;">
</div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;">In my case, the first query returns just 9 customers named Smith (from total of 10 000) and 30 orders (from total of 30 000) for all 9 "Smiths".</span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><br /></span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;">Okay, we have SQL commands - but how can we check their <b>execution time</b> on a database? PostgreSQL provides two commands that show details about executed commands:</span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><b>a) </b>EXPLAIN SQL_command</span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><b>b)</b> EXPLAIN ANALYZE SQL_command - this command is a variant of the first with small difference: examined query is executed and EXPLAIN ANALYZE returns the real execuion time.</span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><br /></span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;">Before we show an example of above commands execution, some technical information about the process of executing the query by PostgreSQL: when the PostgreSQL server receives SQL command from client application, the <b>query string</b> is passed to the <b>query parser</b> that checks for syntax errors. If everything is ok, parser based on the query creates a structure called the <b>query tree</b>. Then, this structure is passed to the <b>query planner</b> which generates all possible <b>query plans</b> and chooses the shortest one from them. This plan is passed to the <b>executor</b> who performs the query and returns the records in the manner described in query plan.</span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;">Mentioned EXPLAIN command displays the details of the plan chosen by the planner as the shortest one. Let's see the result of executing EXPLAIN ANALYZE for the queries about users Smith:</span><br />
<span style="font-family: inherit;"><br /></span></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjp6Vi9gqdzqF7JjxCxqPuXdna8AWFTXPDU6rLlrSqkoOAFwCieLM0LTONy0IWkP7zTwItc378IdJzqOttEDY5pl9Nx7QZCLmeslIeqRs81uOQJBkyn7UrAK8R69TCct1biT2wAI0sqNU0Y/s1600/2010.01.05_02.PNG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><span style="font-family: inherit;"><img border="0" height="137" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjp6Vi9gqdzqF7JjxCxqPuXdna8AWFTXPDU6rLlrSqkoOAFwCieLM0LTONy0IWkP7zTwItc378IdJzqOttEDY5pl9Nx7QZCLmeslIeqRs81uOQJBkyn7UrAK8R69TCct1biT2wAI0sqNU0Y/s400/2010.01.05_02.PNG" width="400" /></span></a></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><br /></span>
<br />
<div style="color: red;">
<div class="separator" style="clear: both; text-align: center;">
</div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj3gO_vHoNqh13vguPviaEDODORlFMqV2iQ1sjBT1bzASxklT6KuIcvn9vqw2bkoUfPnEi94Bh1qt1P6JUKsJHQf46_iX4cgNnXN1YgvYiopt2K1VlKgaueFHRWakTxXuAPh9xHM4w0Zbun/s1600/2010.01.05_01.PNG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><span style="font-family: inherit;"><img border="0" height="111" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj3gO_vHoNqh13vguPviaEDODORlFMqV2iQ1sjBT1bzASxklT6KuIcvn9vqw2bkoUfPnEi94Bh1qt1P6JUKsJHQf46_iX4cgNnXN1YgvYiopt2K1VlKgaueFHRWakTxXuAPh9xHM4w0Zbun/s400/2010.01.05_01.PNG" width="400" /></span></a></div>
</div>
</div>
<div style="font-family: Verdana,sans-serif;">
</div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><br /></span></div>
<div style="color: red;">
</div>
<div style="font-family: Verdana,sans-serif;">
<div style="color: red;">
<span style="font-family: inherit;"><a href="http://4.bp.blogspot.com/_JXxdCCKdTo0/S2XFx-iB64I/AAAAAAAAEXQ/xPOM4T790Wc/s1600-h/PostgreSQL_smith_orders_basic.PNG"></a></span></div>
<div class="separator" style="clear: both; color: red; text-align: center;">
</div>
<span style="font-family: inherit;">Because reading query plans and their details is a separate art, we will focus on the actual run time (visible as a <b>Total Runtime</b>, respectively <b>1,482 ms for the first question</b>, and <b>16,552 ms for the second question</b>).</span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><br /></span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;">Lets provide <b>indexes</b> for the fields we are searching for and for the fields where the join operation is performed. In our case it will be:</span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><b>a)</b> the <b>lname</b> column of the Customers table - create index with the command:<b> </b></span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><br /></span></div>
<pre class="prettyprint" style="font-family: Verdana,sans-serif;">CREATE INDEX lname_index ON customers USING btree (lname);</pre>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><br /></span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><b>b)</b> the foreign key column in Orders table (the column <b>cutomer_id</b>) - create an index using the command: </span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><br /></span></div>
<pre class="prettyprint" style="font-family: Verdana,sans-serif;">CREATE INDEX customer_fk_index ON orders USING btree (customer_id);</pre>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><br /></span></div>
<div style="font-family: Verdana,sans-serif;">
<b style="color: red;"><span style="font-family: inherit;">Note:</span></b></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;">After You create indexes, You have to <b>refresh the statistics used by database planner</b> to calculate the optimal query plan. This is done by running <b>VACUUM ANALYZE</b> command. After executing this command, once again execute the EXPLAIN ANALYZE command for our "Smith" queries:</span><br />
<span style="font-family: inherit;"><br /></span></div>
<div style="font-family: Verdana,sans-serif;">
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhhCu-euHSyKKO3wbK8w3h-s1MgJXQlkC0QXOX-MJI6zKzgnE87U7GnKh2fbaWpGZ0f10Cp9SZbuMaS-It0KkLMQ7LLB9Q7i2xFQo_YPhkjmMLFFUYX8ISiYRjNCtevjqklwGlvUYzulNTW/s1600/2010.01.05_03.PNG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><span style="font-family: inherit;"><img border="0" height="97" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhhCu-euHSyKKO3wbK8w3h-s1MgJXQlkC0QXOX-MJI6zKzgnE87U7GnKh2fbaWpGZ0f10Cp9SZbuMaS-It0KkLMQ7LLB9Q7i2xFQo_YPhkjmMLFFUYX8ISiYRjNCtevjqklwGlvUYzulNTW/s320/2010.01.05_03.PNG" width="320" /></span></a></div>
<span style="font-family: inherit;"><br /></span>
<br />
<div style="text-align: center;">
<span style="font-family: inherit;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjSKnpe63NjKpit37u9jnoMzAG7VFJFy0GLbw-jteln-OtHodjVnxgABDa2IZaC6iD44UTPiWOqxulJ6XtbnHE48u8vlgv03LhAPiHi3czXHj8VjEH1D9HKF1ZvcEprr60vTmaGLR67oBLq/s1600/2010.01.05_04.PNG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="123" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjSKnpe63NjKpit37u9jnoMzAG7VFJFy0GLbw-jteln-OtHodjVnxgABDa2IZaC6iD44UTPiWOqxulJ6XtbnHE48u8vlgv03LhAPiHi3czXHj8VjEH1D9HKF1ZvcEprr60vTmaGLR67oBLq/s320/2010.01.05_04.PNG" width="320" /></a> </span></div>
<span style="font-family: inherit;"><br /></span>
<span style="font-family: inherit;">The results of the queries which are <b>several times faster than previously</b>. They are respectively: <b>0,123 ms (1,482 ms previously)</b>, and<b> 0,507 ms (16,552 ms previously)</b>.</span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><br /></span>
<span style="font-family: inherit;">The advantage is obvious. But whether the index will solve all our problems? Not always. There is no guarantee that the planner will benefit from the index. Sometimes it appears that the sequential table scan can be faster than using an index (such behavior may depend on the "selectivity" degree of certain query). In most cases it is good to have indexes - they can only help.</span></div>
<div style="font-family: Verdana,sans-serif;">
<br /></div>
Paweł Nieściorukhttp://www.blogger.com/profile/12640446237233312925noreply@blogger.com0tag:blogger.com,1999:blog-160318223199333541.post-47818095601137734342009-10-12T20:18:00.256+02:002018-12-16T12:58:05.901+01:00"My database is slow" - part 1<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><b>Note: </b></span>below <span style="font-family: inherit;">description uses <b>PostgreSQL 8.3.7.</b></span><br />
<span style="font-family: inherit;"><br /></span>
<span style="font-family: inherit;">Perhaps You often heard complaints that some application is veeeery sloooow. Despite the technology used, bought another X GB of RAM and so on the problem still exists. Sometimes the cause can be very simple - <b>interaction with the database can be a bottleneck</b> and a source of all problems. This can be checked and possibly improved.</span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;">Often the developer itself must change something in the database - do not be afraid to say it - hire a database magician cost a lot of money for the company and often the developer becomes a database specialist ;-). Therefore, anyone who changes something in the database must have some knowledge. Otherwise, some ad-hoc changes can cause a lot of performance problems.</span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;">What can you do then? Having the database, You can take a closer look on some points:</span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><br /></span></div>
<div style="font-family: Verdana,sans-serif;">
<b><span style="font-family: inherit;">1. Database normalization (decomposition).</span></b></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><br /></span>
<span style="font-family: inherit;">Simply said - making a database to be in the <b>n-th normal form</b>. You may naively believe that "the database engine can handle" somehow our SQL query, without worrying ourselves about performance. Theoretically yes, even nightmarish database schema will work somehow - after all, a bit smarter (than the rest) people formed the principles of relational databases. The problem starts when our application is no longer a primitive data browser which executes the same "select" SQL query all the time. That's why the other smarter people have invented a normalization. For example this allows the database engine not to retrieve 20 columns from one table just to show us only those 2 which we are interested.</span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;">For example let's assume that we have a company and we store <b>all the employees in a one big table</b>: </span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><br /></span></div>
<pre class="prettyprint" style="font-family: Verdana,sans-serif;">CREATE TABLE employees (
id int4 PRIMARY KEY,
active boolean,
first_name varchar,
middle_name varchar,
last_name varchar,
ssn varchar,
address1 varchar,
address2 varchar,
city varchar,
state varchar(2),
zip varchar,
home_phone varchar,
work_phone varchar,
cell_phone varchar,
fax_phone varchar,
pager_number varchar,
business_email varchar,
personal_email varchar,
salary int4,
vacation_days int2,
sick_days int2,
employee_number int4,
office_addr_1 varchar,
office_addr_2 varchar,
office_city varchar,
office_state varchar(2),
office_zip varchar,
department varchar,
title varchar,
supervisor_id int4
);
</pre>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><br /></span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;">And now some Secretary wants to find those <b>people who earn more than 5000</b> USD:</span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><br /></span></div>
<pre class="prettyprint" style="font-family: Verdana,sans-serif;">SELECT first_name, last_name from employees where salary > 5000 ;</pre>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><br /></span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;">Let's assume that the company is a big corporation which employs thousands of people. Database engine must scan entire table, retrieve all columns for people who earn more than 5000 and at the end pefrorm a projection and return only 2 columns - first name and last name.</span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><br /></span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;">Note: do not kid ourselves - <b>normalization is not an ideal remedy for performance problems</b>, and sometimes may even contribute to decrease a performance. Why? For example let's take huge reporting queries that collect a lot of cross-sectional data. In the fully normalized database, the amount of join operations between tables can be very large and could have significant impact on performance. Sometimes it is better to have non-normalized parts of database schema, or even have non-normalized redundant data tables (for generating reports) next to normalized parts. There is <b>no silver bullet solution</b> for that - the approach depends on specific problem.<b> </b></span><br />
<span style="font-family: inherit;"><br /></span>
<span style="font-family: inherit;"><b>2. Memory configuration for the server. </b> </span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><br /></span>
<span style="font-family: inherit;">The fact that Your hardware allows You to play latest games, does not mean that the database will work fast. Even 10-th normal form of a database schema and super-optimal queries will not help when server has total memory similar to required by old DOS games. Sooner or later because of lack of memory many I/O disk operation will be performed to compensate memory request. System resources are important. Unfortunately, at this point databases vary widely in terms of available configuration options. "<b>No silver bullet</b>" also applies here - the only way to discover best performance is to tune database memory settings and test it against SQL queries.<b> </b></span><br />
<span style="font-family: inherit;"><br /></span>
<b><span style="font-family: inherit;">3. Opening connection to the database.</span></b><br />
<span style="font-family: inherit;"><br /></span>
<span style="font-family: inherit;">Although the authors of JDBC drivers maintain that connection cost is small and well optimized, we must not forget that after all this is some expense. As an example, consider a typical web application where in every moment a user clicks on different screens, which show him various data. We have to approaches here:</span></div>
<div style="font-family: Verdana,sans-serif;">
<ul>
<li><span style="font-family: inherit;"><b></b>maintaining an open connection during user's working time</span></li>
<li><span style="font-family: inherit;">opening connection for each request, performing some database query, returns the result (which is presented to the user) and closing the connection.</span></li>
</ul>
</div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;">First approach is expensive (in terms of resources and performance), second is much better. But when we take into account the large number of users and their actions, total cost of opening and closing connections may have considerable importance. It is worth here to consider using the so-called <b>connection pool</b> - such as Apache DBCP or C3P0. Responsibility for creating/closing of connections goes to connection pool. The pool itself has various configuration options that can increase the efficiency of cooperation with the database application. For example some connections may have been opened after startup and ready to immediate use and so on.<b> </b></span><br />
<span style="font-family: inherit;"><br /></span>
<b><span style="font-family: inherit;">4. Transaction isolation levels and locks.</span></b><br />
<span style="font-family: inherit;"><br /></span>
<span style="font-family: inherit;">Transaction isolation levels have a direct impact on the types of locks used. If You are not sure how to choose the level of transaction isolation, it is better to remain at the default level for a particular database. You have to remember one important thing: the higher isolation level you choose the more subtle and complex lock must be use by the database engine to provide concurrent access to data. Using higher levels of isolation can also cause <span class="short_text" id="result_box"><span title="">deadlock occurrence. </span></span></span></div>
<span style="font-family: inherit;"><br /></span>
<br />
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><b>Note: </b></span><span style="font-family: inherit;">You may not agree with mentioned points. They are only my personal subjective choice based on problems and observations during my work. Database performance is a very broad issue dependent on many factors - I pointed only four general issues, but You may find another four or more which will be just as important and necessary.</span><br />
<div style="font-family: verdana, sans-serif;">
<br /></div>
</div>
Paweł Nieściorukhttp://www.blogger.com/profile/12640446237233312925noreply@blogger.com0tag:blogger.com,1999:blog-160318223199333541.post-25139188424968323452009-05-04T20:37:00.174+02:002017-02-10T10:55:28.624+01:00Installing and configuring Tomcat for Eclipse<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><b>Note: b</b>elow description uses <b>Tomcat 7.0.40.</b></span><br />
<span style="font-family: inherit;"><br /></span>
<span style="font-family: inherit;">Probably You always wanted to <b>write your own web-based application in Java</b> (ok, read: writing another Facebook-like portal and live in luxury in Dubai to the end of Your life ;-)). Of course it is possible, but before that inevitable moment occurs, You have to do much more simple thing - <b>configure the environment for developing web applications</b> (unless You are headmaster in Your own company and someone else does it for you...).</span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><br /></span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;">So let's look at how to configure <b>Apache Tomcat</b> which is professionally defined as the <b>servlet container</b> (i.e. for JSP). I will show how to configure it under Eclipse.</span></div>
<div style="font-family: Verdana,sans-serif;">
</div>
<span style="font-family: inherit;"><br /></span>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;">Recall that so far we have managed to <a href="http://technology-for-human.blogspot.com/2009/04/instalacja-java-jdk-prawie-bezbolesna.html">install Java</a> and <a href="http://technology-for-human.blogspot.com/2009/04/instalacja-i-konfiguracja-ide-eclipse.html">set up Eclipse</a> to work with it. It is time for Tomcat. </span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><br /></span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><b>Step 1:</b> download the Tomcat binary distribution (core version as a .zip file) from <a href="http://tomcat.apache.org/download-70.cgi">here</a><b></b></span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><br /></span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><b>Step 2:</b> Unpack the .zip from step 1 to the <b>C:\Development\Tomcat </b>directory. We should get something like this:</span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><br /></span>
<div class="separator" style="clear: both; text-align: center;">
</div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj8feaEBPIJVSXFY3Wa9zDTBOX_SyGyGg5jyl1Mc6VwQOjiyL3weV0yCgUeip7qeK2eHwNfmM_XvoA1z1mobvbVviOoUzGF6CVYA7Xa0MDVRLMo0Mc283cJcflWNsVxQbzuYOeAg8Qe-flj/s1600/2009.05.04_01.PNG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><span style="font-family: inherit;"><img border="0" height="298" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj8feaEBPIJVSXFY3Wa9zDTBOX_SyGyGg5jyl1Mc6VwQOjiyL3weV0yCgUeip7qeK2eHwNfmM_XvoA1z1mobvbVviOoUzGF6CVYA7Xa0MDVRLMo0Mc283cJcflWNsVxQbzuYOeAg8Qe-flj/s320/2009.05.04_01.PNG" width="320" /></span></a></div>
<span style="font-family: inherit;"><br /></span></div>
<div style="font-family: Verdana,sans-serif;">
</div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><b>Step 3:</b> Open Eclipse IDE and the Java EE perspective (Window menu -> Open Perspective -> Other -> Java EE). This will be the default perspective for our work. Using this perspective, at the bottom in the "Servers" tab we add a new server:</span><br />
<span style="font-family: inherit;"><br /></span></div>
<div style="font-family: Verdana,sans-serif;">
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEit7kW-ZFojTLKAVJwYIL7JaFiPSMWGFyxF3w_VQpwDNRDrAHABQT-Z8Xdz7P4Qe4xkSkFxkvOTjPgzI0k9rHkg5LncdmKc8pFTuKI9wmPirmWVihLsshciLU9qAWD6MSYn-w-ReBZm0HBr/s1600/2009.05.04_02.PNG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><span style="font-family: inherit;"><img border="0" height="180" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEit7kW-ZFojTLKAVJwYIL7JaFiPSMWGFyxF3w_VQpwDNRDrAHABQT-Z8Xdz7P4Qe4xkSkFxkvOTjPgzI0k9rHkg5LncdmKc8pFTuKI9wmPirmWVihLsshciLU9qAWD6MSYn-w-ReBZm0HBr/s400/2009.05.04_02.PNG" width="400" /></span></a></div>
<span style="font-family: inherit;"><br /></span>
<div style="color: red;">
<div class="separator" style="clear: both; text-align: center;">
</div>
</div>
</div>
<div style="font-family: Verdana,sans-serif;">
</div>
<div style="font-family: Verdana,sans-serif;">
</div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><b>Step 4:</b> Configure the new server. Select the type of the server (Tomcat 7) and leave the name set to localhost:</span></div>
<div style="font-family: Verdana,sans-serif;">
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiK5L1XpfTD6iVoEUGaE7czLArQ3aCu9OGGHX_wLJASE7tn4_x9SvHIWAp0XlOF-WSkVkNs61HejYagwBUxgss5cNCENyYlCTvKVpo-pTbJSSE4brM3yuJ_2LCdrOHW2s6e0bcVn9dRdlN8/s1600/2009.05.04_03.PNG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><span style="font-family: inherit;"><img border="0" height="400" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiK5L1XpfTD6iVoEUGaE7czLArQ3aCu9OGGHX_wLJASE7tn4_x9SvHIWAp0XlOF-WSkVkNs61HejYagwBUxgss5cNCENyYlCTvKVpo-pTbJSSE4brM3yuJ_2LCdrOHW2s6e0bcVn9dRdlN8/s400/2009.05.04_03.PNG" width="351" /></span></a></div>
</div>
<div style="font-family: Verdana,sans-serif;">
</div>
<div style="font-family: Verdana,sans-serif;">
<div class="separator" style="clear: both; text-align: center;">
</div>
<span style="font-family: inherit;"><br /></span>
<span style="font-family: inherit;"><b>Step 5:</b> Further configuration of the new server. Select the server's installation directory (in our case C:\Development\Tomcat) and Java virtual machine which will be used - in this case it will be the same virtual machine which we configured to work with Eclipse (see this <a href="http://technology-for-human.blogspot.com/2009/04/instalacja-i-konfiguracja-ide-eclipse.html">article</a>).</span></div>
<div style="font-family: Verdana,sans-serif;">
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiJEoGbZhu7XTIf5fBwaBs_Y6ASmaw8Xi7ceKUbvvNgipN2Z-KcJ8VDWjnph9YnQVvSvzforOSB3DxcXgZIep__04n4TTl2Ux0QjcS7JqJHfKizXoRIAV6OOItsKgrDTtxwkSJpfRPaSUFE/s1600/2009.05.04_04.PNG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><span style="font-family: inherit;"><img border="0" height="320" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiJEoGbZhu7XTIf5fBwaBs_Y6ASmaw8Xi7ceKUbvvNgipN2Z-KcJ8VDWjnph9YnQVvSvzforOSB3DxcXgZIep__04n4TTl2Ux0QjcS7JqJHfKizXoRIAV6OOItsKgrDTtxwkSJpfRPaSUFE/s320/2009.05.04_04.PNG" width="281" /></span></a></div>
<div class="separator" style="clear: both; text-align: center;">
</div>
<div class="separator" style="clear: both; text-align: left;">
</div>
</div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><br /></span>
<span style="font-family: inherit;">After pressing Next, Eclipse will display a window asking You to add some web applications to the newly defined server. Ignore this by pressing Finish.</span><br />
<span style="font-family: inherit;"><br /></span></div>
<div style="font-family: Verdana,sans-serif;">
</div>
<div style="font-family: Verdana,sans-serif;">
</div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><b>Step 6:</b> Basic configuration of a server. Open server configuration panel by double click on the server name on Servers tab.</span><br />
<span style="font-family: inherit;"><br /></span></div>
<div style="text-align: left;">
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjJOntlHZcY2mJxBGoKsnmjPsyRUvkGmcvVlu3TBEtsLkDT5y7YdmnukDdTaWV0rvoinwomjpp_ii2qKjgzMyFCLtXt2KCOqTGftjKumzsoKDdJ0rD4hDFvi42f5J5u7iCu8VFSTCic3n0Y/s1600/2009.05.04_05.PNG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><span style="font-family: inherit;"><img border="0" height="281" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjJOntlHZcY2mJxBGoKsnmjPsyRUvkGmcvVlu3TBEtsLkDT5y7YdmnukDdTaWV0rvoinwomjpp_ii2qKjgzMyFCLtXt2KCOqTGftjKumzsoKDdJ0rD4hDFvi42f5J5u7iCu8VFSTCic3n0Y/s400/2009.05.04_05.PNG" width="400" /></span></a></div>
<span style="font-family: inherit;"><br /></span>
<span style="font-family: inherit;">We allow the Eclipse to manage Tomcat installation and use <b>wtpwebapps</b> folder (in Tomcat directory structure) for deploying our applications. In addition we change the way Tomcat publish applications on the server - each application will have a separate xml configuration file in Tomcat (so-called "context" file).</span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;"><br /></span></div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-family: inherit;">Tomcat is <b>ready and configured to work</b>. We can start and stop it using the icons on the right side in the "Servers" tab. The results of these operations (logs), are visible in the "Console" tab.</span></div>
Paweł Nieściorukhttp://www.blogger.com/profile/12640446237233312925noreply@blogger.com0