jsp中表单提交方法和获取对应值,jsp登录页面

来源:互联网 发布:网站数据安全如何保证 编辑:程序博客网 时间:2024/06/05 16:34

下面是登录页面。

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"    pageEncoding="ISO-8859-1"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>Insert title here</title></head><body><form action="LoginServlet" method="post">User :<input type="text" name="username"/><br>Password : <input type="password" name="password" /><br><input type="submit" value="Submit"/></form></body></html>



web.xml页面:
<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">  <display-name>day1_14_2</display-name>  <servlet>    <description></description>    <servlet-name>LoginServlet</servlet-name>    <servlet-class>com.login.LoginServlet</servlet-class>  </servlet>  <servlet-mapping>    <servlet-name>LoginServlet</servlet-name>    <url-pattern>/LoginServlet</url-pattern>  </servlet-mapping></web-app>



LoginServlet.java页面:
package com.login;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 LoginServlet extends HttpServlet {private static final long serialVersionUID = 1L;protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {this.doPost(request, response);}protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {String username = request.getParameter("username");String password = request.getParameter("password");System.out.println("获取到的用户名和密码为:"+username+password);}}


用from表单post提交到loginServlet页面的用request接收。

接收单独的名和值可以用request.getParameter();即可;

接受一个名对应多个值的可以用request.getparamentvalues();方法;

代码如下:
1、登录页面:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"    pageEncoding="ISO-8859-1"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>Insert title here</title></head><body><form action="LoginServlet" method="post">User :<input type="text" name="username"/><br>Password : <input type="password" name="password" /><br>interesting:<input type="checkbox" name="inseresting" value="Reading" /> Reading<input type="checkbox" name="inseresting" value="Game" /> Game<input type="checkbox" name="inseresting" value="Shopping" /> Shopping<br><br><input type="submit" value="Submit"/></form></body></html>


2、web.xml页面(同上,无改变):
<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">  <display-name>day1_14_2</display-name>  <servlet>    <description></description>    <servlet-name>LoginServlet</servlet-name>    <servlet-class>com.login.LoginServlet</servlet-class>  </servlet>  <servlet-mapping>    <servlet-name>LoginServlet</servlet-name>    <url-pattern>/LoginServlet</url-pattern>  </servlet-mapping></web-app>

3、 LoginServlet.java页面:
package com.login;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 LoginServlet extends HttpServlet {private static final long serialVersionUID = 1L;protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {this.doPost(request, response);}protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {String username = request.getParameter("username");String password = request.getParameter("password");System.out.println("获取到的用户名和密码为:"+username+password);String [] interestings = request.getParameterValues("inseresting");for(String interest:interestings){System.out.println("兴趣:"+interest);}}}



0 0
原创粉丝点击