表單文件: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: <input name="userName" type="text"/><br/>
password: <input name="password" type="password"/><br/>
<input type="submit" value="submit"/> <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”要與<inputname="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。