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)

Source: Internet
Author: User

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)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.