Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jsp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jsp 如何通过标记“将值从查看器传递到控制器”;a「;_Jsp_Jakarta Ee_Spring Mvc - Fatal编程技术网

Jsp 如何通过标记“将值从查看器传递到控制器”;a「;

Jsp 如何通过标记“将值从查看器传递到控制器”;a「;,jsp,jakarta-ee,spring-mvc,Jsp,Jakarta Ee,Spring Mvc,我想通过标记“a”将值从查看器发送到控制器,而不使用“form”标记 在我的查看器I代码中: <input type="text" id="stuid"/> <input type="text" id="stuname"/> <a href="student.html? //something here that I don't know... 你可以试试这个 <HTML> <

我想通过标记“a”将值从查看器发送到控制器,而不使用“form”标记

在我的查看器I代码中:

           <input type="text" id="stuid"/>
           <input type="text" id="stuname"/>

           <a href="student.html? //something here that I don't know...

你可以试试这个

<HTML> 
<HEAD> 
<SCRIPT language="JavaScript"> function test() 
{ var p=document.getElementById("stuid").value;
    window.open("sample.html?val="+p,'_self'); 
} 
</SCRIPT> 
</HEAD> 
<BODY>
<input type="text" id="stuid" name="stuid"/> 
<input type="text" id="stuname" name="stuname"/> 
<A HREF="javascript:test()">Test</a>
</BODY> 
</HTML>

功能测试()
{var p=document.getElementById(“stuid”).value;
window.open(“sample.html?val=“+p”,“u self”);
} 

您可以通过链接传递参数,如下所示:

<a href="student.html?name=John&age=21">Student</a>

我建议您使用Ajax和jQuery,例如,我认为您不必使用表单标记

就必须从JSP传递3个参数到servlet。 index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>Index Page</title>

<script src="http://code.jquery.com/jquery-1.10.2.js" type="text/javascript"></script>
<script src="js/app-ajax.js" type="text/javascript"></script>
</head>
<body>

        Enter Your Name: <input type="text" id="name" /><br>
        Enter our user name :<input type="text" id="userName"><br>
        Enter your Password :<input type="password" id="password"><br>
        <input type="button" value="Submit" onclick="ajaxCall();">        
    <br>

    <strong> Response</strong>:
    <div id="ajaxGetUserServletResponse"></div><br>
    <div id="ajaxGetUserServletResponseusername"></div><br>
    <div id="ajaxGetUserServletResponsepassword"></div><br>
</body>
</html>
GetUserServlet.java

package com.logic;

import java.io.IOException;

import java.util.ArrayList;

import javax.management.relation.RelationSupportMBean;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class GetUserServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

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

        userBean ub = new userBean();
        String name = request.getParameter("name").trim();
        String userName = request.getParameter("userName").trim();
        String password = request.getParameter("password").trim();

        System.out.println("name catched "+name);
        System.out.println("username catched"+userName);
        System.out.println("Password catched"+password);


        ArrayList<userBean> list = new ArrayList<userBean>();
         ub=new userBean();
         ub.setName(name);
         ub.setUserName(userName);
         ub.setPassword(password);
         list.add(ub);  

         response.setContentType("text/plain");
         response.getWriter().print(list);

    }

}
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_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>AjaxJspServlet</display-name>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>

<servlet>
<servlet-name>one</servlet-name>
<servlet-class>com.logic.GetUserServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>one</servlet-name>
<url-pattern>/GetUserServlet</url-pattern>
</servlet-mapping>

</web-app>

AjaxJspServlet
index.jsp
一
com.logic.GetUserServlet
一
/GetUserServlet

我应该在控制器中做什么?创建一个名为val的变量,并为其创建一个getter和setter方法。您将在val中获得值。如果它对您有效,请不要忘记将其标记为答案。您可以编写有关这些的详细代码吗?如何从HMTL控件获取值并使用链接附加它们的值?
package com.logic;

import java.io.IOException;

import java.util.ArrayList;

import javax.management.relation.RelationSupportMBean;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class GetUserServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

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

        userBean ub = new userBean();
        String name = request.getParameter("name").trim();
        String userName = request.getParameter("userName").trim();
        String password = request.getParameter("password").trim();

        System.out.println("name catched "+name);
        System.out.println("username catched"+userName);
        System.out.println("Password catched"+password);


        ArrayList<userBean> list = new ArrayList<userBean>();
         ub=new userBean();
         ub.setName(name);
         ub.setUserName(userName);
         ub.setPassword(password);
         list.add(ub);  

         response.setContentType("text/plain");
         response.getWriter().print(list);

    }

}
package com.logic;

public class userBean 
{
    private String name;
    private String userName;
    private String password;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getUserName() {
        return userName;
    }
    public void setUserName(String userName) {
        this.userName = userName;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }

}
<?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_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>AjaxJspServlet</display-name>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>

<servlet>
<servlet-name>one</servlet-name>
<servlet-class>com.logic.GetUserServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>one</servlet-name>
<url-pattern>/GetUserServlet</url-pattern>
</servlet-mapping>

</web-app>