The question that this article solves is how to introduce our familiar httpservletrequest and Httpservletrespon in the business method?
Answer: This practice of introducing traditional Web parameters is not recommended because it is highly coupled.
But let's talk about this approach:
The action modifies the code as follows:
Packagecom.guigu.shen.Action7;Importjava.io.IOException;Importjavax.servlet.http.HttpServletRequest;ImportJavax.servlet.http.HttpServletResponse;ImportOrg.springframework.stereotype.Controller;ImportOrg.springframework.ui.Model;Importorg.springframework.web.bind.annotation.RequestMapping;ImportOrg.springframework.web.bind.annotation.RequestMethod;/*** * Request path can be split into: Root Module name + sub-module name is equivalent to when access to http://127.0.0.1:8080: project name/user/register will enter the Registermethod method. */@Controller @requestmapping (value= "/user")//request name of the root module Public classuseraction {/** Employee Registration **/@RequestMapping (Method=requestmethod.post,value= "/register")//request name of the sub-module PublicString Registermethod (httpservletrequest httpservletrequest,httpservletresponse httpservletresponse) {/*data is obtained through HttpServletRequest and Httpservletrespon. */String username=httpservletrequest.getparameter ("username"); String Salery=httpservletrequest.getparameter ("Salary"); //save to session levelHttpservletrequest.getsession (). SetAttribute ("username", username); Httpservletrequest.getsession (). SetAttribute ("Salary", salery);// redirect Try{httpservletresponse.sendredirect (Httpservletrequest.getcontextpath ()+ "/jsp/success.jsp"); System.out.println ("L path is" +Httpservletrequest.getcontextpath ()); } Catch(IOException e) {//TODO auto-generated Catch blocke.printstacktrace ();} return NULL;}}
The success.jsp code is as follows:
<%@ Page Language="Java"Import="java.util.*"pageencoding="UTF-8"%><%@ taglib URI="http://java.sun.com/jsp/jstl/fmt"prefix="FMT" %><%StringPath=Request.getcontextpath ();StringBasePath=Request.getscheme ()+"://"+Request.getservername ()+":"+Request.getserverport ()+Path+"/";%><!DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en"><HTML> <Head> <Basehref= "<%=basePath%>"> <title>My JSP ' index.jsp ' starting page</title> <Metahttp-equiv= "Pragma"content= "No-cache"> <Metahttp-equiv= "Cache-control"content= "No-cache"> <Metahttp-equiv= "Expires"content= "0"> <Metahttp-equiv= "keywords"content= "Keyword1,keyword2,keyword3"> <Metahttp-equiv= "description"content= "This is my page"> <!--<link rel= "stylesheet" type= "Text/css " href= "Styles.css" > - </Head> <Body>Success.<BR> <%--output The value of the previous page input--%>${username} ${salary}</Body></HTML>
The results are as follows:
Success.
AAA 1000
14SPRINGMVC_ write traditional Web parameters such as Httpservletrequest,httpservletresponse in the Business control method (This knowledge point is good to know, not recommended to do so)