jsp+servlet 表单验证

<%@ page Xlanguage="java" import="java.util.*" pageEncoding="utf-8"%>



My JSP 'test.jsp' starting page










表单提交









name:
sex:  女
address:
likes:singdancegame
 




package com.csdn.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class testresult extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

response.setContentType("text/html;charset=utf-8");
request.setCharacterEncoding("utf-8");

String name = request.getParameter("username");
String sex = request.getParameter("sex");
String address = request.getParameter("address");
String[] likes = request.getParameterValues("likes");
String likes1="";
if(likes!=null){
for(String like:likes){
likes1 +=like+" ";
}
}
String url="/test.jsp";
if(name!=null && !name.equals("")){
request.setAttribute("username", name);
request.setAttribute("sex", sex);
request.setAttribute("address", address);
request.setAttribute("likes",likes1);
url="/testresult.jsp";
}
request.getRequestDispatcher(url).forward(request, response);
}

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

doGet(request, response);
}

}
<%@ page Xlanguage="java" import="java.util.*" pageEncoding="utf-8"%>





My JSP 'testresult.jsp' starting page









<%=request.getAttribute("username")%>
<%=request.getAttribute("sex")%>
<%=request.getAttribute("address")%>
<%=request.getAttribute("likes")%>


你可能感兴趣的:(jsp+servlet 表单验证)