SpringMVC的表單提交的例子和session運(yùn)用的例子

表單提交和session 
馬克- to-win:馬克 java社區(qū):防盜版實(shí)名手機(jī)尾號(hào): 73203。
像學(xué)servlet那時(shí)一樣,繼hello world的例子以后,緊接著我們就要學(xué)習(xí)表單提交和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 {
//沒(méi)用        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";//此句可以工作
    }
}