Sie sind auf Seite 1von 3

Hello World Example using Spring MVC Framework and Jasypt.

For encrypting the password use the Jasypt Command line interface

C:\jasypt-1.5\bin>encrypt.bat input=" memberdev_password" password=jk08us12Nk verbose=false


QF8JiowwXRztdVPxx0GX4JrqpKokhBCDOC4cfmNW2fo=

application-jdbc.properties -- Have the properties that we will access with our web application, in this
file we have given the password in the encrypted form and we have to use ENC(…) to be able to read by
Jasypt.

username=memberdev
password=ENC(QF8JiowwXRztdVPxx0GX4JrqpKokhBCDOC4cfmNW2fo=)

The Encryption password can be set as an environment variable or as a System property. To add the JVM
parameter for tomcat as in going in eclipsepreferences Tomcat JVM Settings > JVM parameters.

-D APP_ENCRYPTION_PASSWORD= jk08us12Nk

Note- APP_ENCRYPTION_PASSWORD can be defined as a system environment variable but it has


some issues with Java 1.4 so if you are using Java 5 you can make use of System environment variable
instead of a Tomcat JVM variable.

WEB-INF: This directory holds all the *.jar files and *.xml files.

index.jsp: The jsp page is the welcome page with a Submit button.
success.jsp: When the application runs fine we will be forwarded to this jsp from index.jsp. We print the
password in this jsp.

The message attribute is set in the controller class.

lib : This directory holds all the jar files that are necessary for running this application. Just copy the
spring.jar,commons-logging.jar and servlet-api.jar files into this lib directory.
Now lets have a look over all these xml files that are required to configure the application

Web.xml - The servlet specified here is DispatcherServlet, the same as ActionServlet we use for Struts.
Hava a look over the servlet name, the spring config xml files must start with hello as like this
'helloservlet.xml' .

hello-servlet.xml -
This xml file specifies the bean which should be invoked for the action passed from index.jsp.
Here we are setting the “password” property that will be viewed on success.jsp page.

<property name="password">
<value>${password}</value>
</property>
The view resolver bean specifies the mapping details for the passed reference from the controller. When
used InternalResourceViewResolver class it check for the files with the name that matches when
returning the ModelAndView object.
This also includes which type of files to check out, Here we specified to check over all *.jsp files
matching the parameter that is passed from the controller.

applicationContext.xml – This is the spring configuration xml file , we have to define the property
configure in this file and give the path of the propery file from where the application will read the
password . Another important thing here is to define the environmental configuration ,
If we have to use System variable we will use a passwordEnvNaame Or if we have to use a JVm
variable we will use passwordSysPropertyName.

<bean id="environmentVariablesConfiguration"
class="org.jasypt.encryption.pbe.config.EnvironmentStringPBEConfig" >
<property name="algorithm" value="PBEWithMD5AndDES" />
<!-- <property name="passwordSysPropertyName"
value="APP_ENCRYPTION_PASSWORD" /> -->
<property name="passwordEnvName" value="APP_ENCRYPTION_PASSWORD" />
</bean>

HelloController.java -- This class implements the main Interface Controller, which is Spring's MVC
main Interface .The ModelAndView class is the one which is used to bind model and view. The object
passed would be including the mapping path for the view, and the greeting variable is bound with
'messages' attribute. This attribute is set into the request object.

The build.xml file, there are different targets defined to build the application like clean, compile, copy-
resources, tomcat-clean and install. Each has its specific task to do and depends on others. Just build it
with ant and it will do all the work for you.

So that’s it, now you have all the java code stuff, all the .xml files and .jsp files.
just build ur application using ant. You can see two directories created 'dist' and 'war' as shown below

Now just restart your tomcat and type the url ' http://localhost:8080/springmvc/ '
you should be able to see the screen as such if the application runs fine..

and when submitted the form on click the button, we get should see the Decrypted password as such in
success.jsp

memberdev_password
Code.zip
Code have been attached as a Zip file with this document.

Das könnte Ihnen auch gefallen