"Welcome To Ashok IT" "Spring Boot and MicroServices" Topic : Spring Boot MVC Date : 09/05/2025 (Session - 43) _____________________________________________________________________________________________________________________________ Steps for Developing the First WebApplication using Spring MVC ============================================================== Step-1 : ======= * Download and install the ApacheTomcat10 version from below url https://tomcat.apache.org/download-90.cgi >>>> select installer service (32-bit/64-bit Windows Service Installer (pgp, sha512)) During the installation change the tomcat server port no from 8080 to 1234 During the installation remember the username & password for tomcat manager i.e.,admin/admin. Step-2 : Adding Tomcat Server into STS IDE ========================================== * Open the STS IDE and Navigate to "Window" Menu >>>> Again Select "Show View" >>> Again select "Others" and Find "Server" >> select "Servers" Option and server window will appear. * Under the Server Section need to Add the Server by clicking on Hyperlink and select "Apache" >>> Find "TomcatV9.0 Server" >>> Click on "Next" >> Point the Apache Tomcat Server 10.1 Location >>> Click on "Finish" button. Step-3 : Creating Project in STS IDE ==================================== * Open STS IDE and create new Maven project by checking checkbox of skiping the archetype selection * Provide below values for artifacts co-ordinates GroupId : com.ashokit ArtifactId :28_SpringMVC_WebApp package :com.ashokit * Click on "Finish" Button and then project will be created succesfully in STS IDE. NOTE ==== * If the below folders are missing in the Maven Project create the folder as below steps * Righ Click on Project folder>>> Select option as "Build Path" >>> Again select "New Source Folder" >>> Provide name as "src/main/java" & "src/main/resource". Step-4 : Configuring the DispatcherServlet in web.xml ===================================================== web.xml ======= AshokITFirstWebApplication ashokit org.springframework.web.servlet.DispatcherServlet 1 ashokit *.do NOTE ==== * We enabled load on startup with positive value for DispatcherServlet and will create an object during the server startup time itself. * We configured the URl pattern as "/" representing will accept any kind of request by DispatcherServlet. Step-5 : Creating Controller class in Spring MVC ================================================ * Create a Java Class and add the annotation as @Controller will becomes as spring bean class in spring mvc. WelcomeController.java ====================== package com.ashokit.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView; @Controller public class WelcomeController { //adding request Processor Method for welcome Message @RequestMapping(value = "welcome") public ModelAndView getWelcomeMessage() { ModelAndView mav = new ModelAndView("welcome"); mav.addObject("wishMessage", "Welcome To AshokIT For Spring MVC WebAplication Development"); return mav; } } Step-6 : Spring Configuration File ================================== * A DispatcherServlet will automaticaly load/read spring configuration file if the following two conditions are satisfied. 1) File name should be "servlet-name-servlet.xml" (spring configuration file(ashokit-servlet.xml)) 2) The file must be placed in "WEB-INF Folder". * If the above conditions are failed need to configure the extra configuration in web.xml Example ======= Spring MVC Dispatcher Servlet org.springframework.web.servlet.DispatcherServlet contextConfigLocation /WEB-INF/config/web-application-config.xml 1 ashokit-servlet.xml =================== Step-7 : Creating the View ========================== * Right click on "webapp" folder and add the new folder as "views". * After creating the above folder then create a new file with name as "welcome.jsp" as below welcome.jsp =========== <%@ page isELIgnored="false" %>
${wishMessage}
Step-8: pom.xml =============== 4.0.0 com.ashokit 28_SpringMVC_WebApplication 0.0.1-SNAPSHOT war UTF-8 17 17 org.springframework spring-webmvc 6.0.11 jakarta.servlet jakarta.servlet-api 5.0.0 provided Step-9 : Run the Application ============================ * Right click on Project and choose "Run As" option >>> again select "Run As Server". * Right click on Added server in server section >>>> Select option as "start" and server will start & deployed successfully. * Onces server got started open your favourite browser and hit request with below url http://localhost:2345/26_SpringMvc_WebApp/wishMessage OUTPUT ===== Welcome To AshokIT For Spring MVC WebAplication Development +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ We Will see how to send the data to the Request Processor Method ================================================================= * Inorder to send the data from client to Server we have multiple sources 1) Adding key-value pairs to the browser Request URL ==================================================== http://localhost:8080/27_SpringMVC_App/welcome >>>>>>>>>>>>> Sending Blank Request to Server without having any data http://localhost:8080/27_SpringMVC_App/welcome?username=mahesh >>>>> Sending Request to Server with One Parameter. http -------> Protocal localhost -----> Current Machine 8080 --------> portno 27_SpringMVC_App ----------> WebApplication Name welcome--------> URL Pattern username=mahesh -----------> Request Parameters(username(key),mahesh(value)) http://localhost:8080/27_SpringMVC_App/welcome?username=mahesh&city=Hyderabad >>>>> Sending Request to Server with Multiple Parameters. ?username=mahesh&city=Hyderabad >>>>>>>> QueryString(Request parameters) RequestParameter Names :username,city RequestParameter Values : mahesh,Hyderabad Reading the Request Parameters in the RequestProcessor method of Controller class ================================================================================= 1) Gathering All Request Parameters into Map Object by using annotation called @RequestParam. public String processLogin(@Requestparam Map requestParams) { } 2) Gathering the Request Parameter independently public String processLogin(@RequestParam("username") String uname, @RequestParam("city") String cityName){ } Problems ======= 1) It is not recommended for sending data via URL because its not good practise sending sensitive information in URL. 2) It is not enduser friendly approach. 2) Sending Data to Server using Html Forms ========================================== * As programmer we need to send one Html form to the user browser. * User has to fill the form with proper data and submit that form. * Onces form got submitted by the user the data of form will sent to Server as Request Parameters(or) Request Body Example Application =================== * We will develop two Request Processor methods 1) Request processor method is for rendering the Html Form i.e., welcome page. 2) Request Processor method is for processing the Html Form Data. enquiry.jsp =========== <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> registration.jsp
Welcome To AshokIT For Spring MVC Application Development...
Name
EmailID
ContactNo
Course
confirmation.jsp ================ <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> confirmation.jsp
Student Name : ${enquiryInfo.firstName}
Course Name : ${enquiryInfo.courses}
Your Enquiry is Received for above Details Admin Team will connect with you shortly.....
EnquiryController.java ====================== package com.ashokit.controller; import java.util.Map; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.servlet.ModelAndView; @Controller public class EnquiryController { @RequestMapping("/") public ModelAndView getEnquiryPage() { return new ModelAndView("enquiry"); } @RequestMapping(value="sendEnquiry",method = RequestMethod.POST) public ModelAndView processEnquiryDetails(@RequestParam Map formData) { ModelAndView mav = new ModelAndView("confirmation"); mav.addObject("enquiryInfo", formData); return mav; } } web.xml ======= AshokITFormSubmission ashokit org.springframework.web.servlet.DispatcherServlet 1 ashokit / ashokit-servlet.xml =================== pom.xml ======= 4.0.0 com.ashokit 29_SpringMVC_FormSubmission_App 0.0.1-SNAPSHOT war UTF-8 17 17 org.springframework spring-webmvc 6.0.11 jakarta.servlet jakarta.servlet-api 5.0.0 provided OUTPUT ====== Request URL : http://localhost:1234/29_SpringMVC_FormSubmission_App/ Will load the enquiry form into browser Fill the form with required details and submit form Acknowlege Message ================== StudentName : Mahesh StudentName : SpringBoot Your Enquiry is Received for above Details Admin Team will connect with you shortly..... Assignment =========== * Completeing the Same Application and test this application form method as "get". * Observe the Request URL in the Browser URL Bar... +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++