1. 程式人生 > >jsp中表單提交方法和獲取對應值,jsp登入頁面

jsp中表單提交方法和獲取對應值,jsp登入頁面


下面是登入頁面。

<%@ 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);
		}
	}

}



相關推薦

jsp中表提交方法獲取對應jsp登入頁面

下面是登入頁面。 <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="I

JSP中表內容實現分頁提交

頁面要求:在註冊頁面中需要完成註冊的資訊比較的多,需要分多頁進行內容填寫。 通過 javascript 設定整個頁面的不同模組的 css (即style屬性)屬性,來決定在點選不同的button 的時候顯示不同的註冊子頁面 , 這樣,頁面 請求並沒有發生改變 , 只是當前網

JSP提交校驗

function validate_channel_info(channelform) { if(channelform.channelname.value=="")

對於JSP中表資料儲存的一種通用方法

引言   J2EE(Java 2 Enterprise Edition)技術已廣泛應用在Web應用開發中,其中的JavaBean、Servlet技術為開發者提供了更為清晰的開發環境,使用JSP技術表現頁面,使用Servlet技術完成大量的業務處理,使用Bean來儲存資料及一些

form表提交圖片引數引數獲取為null

問題描述:form表單提交圖片(form中含有屬性enctype="multipart/form-data"才能提交檔案)和其他一些普通引數,發現在後臺獲取引數為null。問題解決:enctype="multipart/form-data"設定了表單以二進位制傳輸。後臺做處理

如何將js獲取的變數付給jsp中表的action

<script > function a(){ var lujing = document.getElementById("bigSort"); var text = lujing.options[lujing.selectedIndex].text;//獲取文

提交postget方法區別

本文轉載於:猿2048網站表單提交post和get方法區別 表象不同,get把提交的資料url可以看到,post看不到

Js 阻止表提交方法

js 阻止表單提交方法<body> <form action="clock.html" method="post" onsubmit="return checkLength()"> <p>name:<input type="text" name

Java web實驗 LoginInfor.jsp session屬性設置獲取

org charset exceptio src req content urn meta tab <%@ page language="java" contentType="text/html; charset=UTF-8"%><!DOCTYPE htm

Java web實驗 station.jsp session屬性設置獲取

web string pass get sta img .get 內容 .com <%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>&

C#通過反射獲取類中的方法參數個數反射調用方法帶參數

new [] 反射 電腦 ram col sta body create using System; using System.Reflection; namespace ConsoleApp2 { class Program { sta

jsp提交前端驗證

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <m

layer父界面調用子彈窗的方法獲取子彈窗的元素總結

return btn iframe nal index child pro win base 1 layer.open({ 2 type: 2 3 ,title: false //不顯示標題欄 4 ,closeBtn

layer父介面呼叫子彈窗的方法獲取子彈窗的元素總結

1 layer.open({ 2 type: 2 3 ,title: false //不顯示標題欄 4 ,closeBtn: false 5 ,area: ['460px', '45%'] 6 ,shade: 0.5

JSP中表加了enctype="multipart/form-data"屬性後request就接收不到表傳過來的的問題

  1.get方式  get方式提交的話,表單項都儲存在http header中,格式是  http://localhost:8080/hello.do?name1=value1&name2=value2這樣的字串。JSP端通過request.getPara

前端表提交資料~php獲取內容

上圖程式碼如下: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xml

mvc提供的ajax表提交方法

Ajax.Beginform()的五個引數 一、actionName 用於指定請求地址的Action名稱。 二、controllerName 用於指定請求地址的Controller名稱。 三、routeValues 用來傳遞引數,支援兩種資料型別(兩種傳參方式):

from表提交方法

#傳統的提交按鈕 #a標籤提交 // 提交訂單 1.無重新整理頁面提交表單 表單可實現無重新整理頁面提交,無需頁面跳轉,如下,通過一個隱藏的iframe實現,form表單的target設定為iframe的name名稱, form提交目標位當前頁面iframe

ligerui 表驗證 隱藏列 獲取對應行中某一列的資料

ar v = $("#PostForm").validt({ //除錯狀態,不會提交資料的 debug: true, rules: { DEPART_ID: 'required', P_DSC: 'required', }, 此處,rules裡的DEPART_ID和P_DS

Servlet的5種方式實現表提交(註冊小功能)後臺獲取資料

用servlet實現一個註冊的小功能 ,後臺獲取資料。 註冊頁面:    註冊頁面程式碼 : <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title&g