请给出一个SpringMVC的表单提交的例子和session运用的例子

2.表单提交和session
像学servlet那时一样,继hello world的例子以后,紧接着我们就要学习表单提交和session。
例2.1
<%@ page contentType="text/html; charset=GBK" %>
<html>
<head>
    <title>form test</title>
</head>
<body>
<%=session.getAttribute("firstN") %>
<FORM ACTION="formHan.do" METHOD="POST">
姓名:
<INPUT TYPE="TEXT" NAME="firstName"><BR>
<INPUT TYPE="SUBMIT" VALUE="Submit">
</CENTER>
</FORM>
</body>
</html>
package com;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class HelloWorldController {
    @RequestMapping("/formHan")
    public void formHandle(HttpServletRequest req, HttpServletResponse res,
            HttpSession ses) throws IOException, ServletException {
//没用        req.setCharacterEncoding("gbk");
        String fn=req.getParameter("firstName");
        System.out.println(fn+"1");
        String fngbk = new String(fn.getBytes("iso8859-1"), "GBK");
        System.out.println("filenameutf is " + fngbk);
        ses.setAttribute("firstN", fngbk);
 //       PrintWriter pw=res.getWriter();
 //       pw.println(fn);//此句可以工作
        req.getRequestDispatcher("formT.jsp").forward(req, res);
 //       res.sendRedirect("formT.jsp");//此句可以工作
//        return "/formT";//此句可以工作
    }
}


更多内容请见原文,文章转载自:https://blog.csdn.net/qq_44639795/article/details/100185314

posted @   malala  阅读(29)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
点击右上角即可分享
微信分享提示