jsp Servlet获取表单提交的内容



jsp Servlet获取表单提交的内容。

获取表单对象的值

工程名:ServletStudy

表单文件:login.jsp

Servlet文件:LoginServlet.java

配置文件:web.xml

下面对是这几个文件的代码:

login.jsp

<%@ page language=“java” import=“java.util.*” pageEncoding=“UTF-8″%>

<%

String path = request.getContextPath();

String basePath = request.getScheme()+”://”+request.getServerName()+”:”+request.getServerPort()+path+”/”;

%>

<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN”>

<html>

<head>

<base href=<%=basePath%>>

<title>My JSP ‘login.jsp’ starting page</title>

<meta http-equiv=“pragma” content=“no-cache”>

<meta http-equiv=“cache-control” content=“no-cache”>

<meta http-equiv=“expires” content=“0″>

<meta http-equiv=“keywords” content=“keyword1,keyword2,keyword3″>

<meta http-equiv=“description” content=“This is my page”>

<!–

<link rel=”stylesheet” type=”text/css” href=”styles.css”>

–>

</head>

<body>

<form action=“/ServletStudy/LoginServlet”>

userName:&nbsp;<input name=“userName” type=“text”/><br/>

password: &nbsp;<input name=“password” type=“password”/><br/>

<input type=“submit” value=“submit”/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type=“reset” value=“reset”/>

</form>

</body>

</html>

LoginServlet.java

package com;

import java.io.IOException;

import java.io.PrintWriter;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

@SuppressWarnings(“serial”)

public class LoginServlet extends HttpServlet

{

protected void doGet(HttpServletRequest req,HttpServletResponse resp)


throws ServletException, IOException

{

String username = req.getParameter(“userName”);

String password = req.getParameter(“password”);

resp.setContentType(“text/html”);

PrintWriter out = resp.getWriter();

out.println(“<html><head><title>Login Result</title></head”);

out.println(“<body> username:”+username+”<br>”);

out.println(“password:”+password+”<br></body></html>”);

out.flush();

}

}

web.xml

在二级标签中添加以下代码:

<servlet>

<description>This is the description of my J2EE component</description>

<display-name>This is the display name of my J2EE component</display-name>

<servlet-name>LoginServlet</servlet-name>

<servlet-class>com.LoginServlet</servlet-class>

</servlet>

<servlet-mapping>

<servlet-name>LoginServlet</servlet-name>

<url-pattern>/LoginServlet</url-pattern>

</servlet-mapping>

说明:

1.<form>标签中的action 值为 web.xml中的<url-pattern></url-pattern>中的值。再加上Servlet类名,如这里的是

/ServletStudy/LoginServlet

提交的方式默认为get方式,本例中即是以get方式提交的,这里没有对中文问题进行处理。但建议一般使用post方式提交表单。

2. String username = req.getParameter(“userName”);

这里的参数值“userName”要与<input name=“userName” type=“text”/>中的name属性值相同

下面是使用doPost提交方式,关键部分代码为:

protected void doPost(HttpServletRequest req,HttpServletResponse resp)

throws ServletException, IOException

{

req.setCharacterEncoding(“utf-8″);

String username = req.getParameter(“userName”);

String password = req.getParameter(“password”);

resp.setContentType(“text/html”);

resp.setCharacterEncoding(“utf-8″); //设置返回给客户端的文本格式,可解决文字编码格式不统一的问题

PrintWriter out = resp.getWriter();

out.println(“<html><head><title>Login Result</title></head”);

out.println(“<body> username:”+username+”<br>”);

out.println(“password:”+password+”<br></body></html>”);

out.flush();

}

 

若使用doPost方式响应客户,则要将<form>表单中的method的值改为post

http://blog.sina.com.cn/s/blog_75855a980100uf86.html