Java 单击按钮时从 JSP 重定向到 servlet

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27846377/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 05:08:52  来源:igfitidea点击:

Redirecting to a servlet from a JSP on button click

javajavascriptjspservlets

提问by Reenu Rahman

Am trying to link from a JSP to a servlet . On clicking the button with the name="conf" I need to redirect to a servlet "/Initial" . The problem is when I use type="button"nothing happens, while when I use type="submit"the page gets directed to servlet "/Initial" and does the action there. Am not able to identify the problem.

我正在尝试从 JSP 链接到 servlet。单击 name="conf" 按钮时,我需要重定向到 servlet "/Initial" 。问题是当我使用时type="button"什么也没有发生,而当我使用type="submit"页面时,页面被定向到 servlet“/Initial”并在那里执行操作。我无法确定问题。

Here is my code:

这是我的代码:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@ page import="reg.serv.*"%>

<!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 method="post">
        <center>
            <table border="1" width="30%" cellpadding="3">
                <thead>
                    <tr>
                        <th colspan="2">Register Here</th>
                    </tr>
                </thead>
                <tbody>
                    <tr>
                        <td>Username</td>
                        <td><input type="text" class="" id="username" name="username1" value="" /></td>
                    </tr>
                    <tr>
                        <td>Password</td>
                        <td><input type="password" name="password1" id="password" value="" /></td>
                    </tr>
                    <tr>
                        <td>Confirm Password</td>
                        <td><input type="password" name="confirmpassword1" id="confirmpassword" value="" /></td>
                    </tr>
                    <tr>
                        <td>Mobile Number</td>
                        <td><input type="text" class="" id="mob" name="mob1" value="" /></td>
                    </tr>
                    <tr>
                        <td>Email ID</td>
                        <td><input type="text" class="" id="email" name="email1" value=" " /></td>
                    </tr>
                    <tr>
                        <td>Address</td>
                        <td><textarea id="address" name="address1"></textarea></td>
                    </tr>

                    <tr>
                        <td colspan="2">Already registered <a href="Index.jsp">Login Here</a></td>
                    </tr>
                </tbody>

                <tr>
                    <td><input type="button" value="confirm" name="conf" /></td>
                    <td><input type="reset" value="Reset" /></td>
                    <td><input type="button" value="Cancel" name="Cr" onclick="openPage('Initial.jsp')" /></td>
                </tr>
        </table>
    </form>

    <script type="text/javascript">

        function openPage(pageURL) {
            window.location = pageURL;
        }

    </script>

    <%
        String x = request.getParameter("conf");

        if (x != null && x.equals("confirm")) {
            //response.sendRedirect("/Initial");
            RequestDispatcher dispatcher = request.getRequestDispatcher("/Initial");
            dispatcher.forward(request, response);
        }
    %>

</body>
</html>


Please help me . Any help would be greatly appreciated. Thanking You.


请帮我 。任何帮助将不胜感激。感谢您。

采纳答案by Ram Mansawala

you have to write

你必须写

<form action=/your_servlet_page_name>

And you have to use

你必须使用

<input type="submit" value="confirm" name="conf"/>

And also you have to map your servlet page into web.xmlfile like

而且您还必须将您的 servlet 页面映射到web.xml文件中,例如

<servlet-mapping>
    <servlet-name>CheckLogin</servlet-name>
    <url-pattern>/CheckLogin</url-pattern>
</servlet-mapping>

回答by greedsin

I dont get exactly what you are trying to do,but redirect is working with: response.sendRedirect(request.getContextPath());or

我不明白你想要做什么,但重定向正在使用: response.sendRedirect(request.getContextPath());

response.sendRedirect(String url);

回答by Arvind Vishwakarma

if you want to use type="button" instead of type="submit". you can use javascript function on the click on the button. Like

如果你想使用 type="button" 而不是 type="submit"。您可以在单击按钮时使用 javascript 功能。喜欢

<script>
function doSubmit(){
var actionURL ="MENTION URL YOU WANT TO REDIRECT";
// perform your operations

myForm.submit(actionURL); OR
myForm.submit();
}

</script>
<form name="myForm">
    <input type="button" name="conf" value="conf" obclick="doSubmit();">
</form> 

hope it will help you.

希望它会帮助你。

回答by Abhijit Bashetti

try by changing the script only

尝试仅更改脚本

<script type="text/javascript">
 function openPage(pageURL)
 {
 window.location.href = pageURL;
 }
</script>

回答by Adzz

<form action = "servlet-name" method = "method in the servlet">
<input type ="submit" value = "val">
</form>

This is a simple way to do this. If you are using the latest jre i think 7 and up, you don't need to declare the servlet in your web.xml file. the @WebServlet("/servlet-url") will do the trick.

这是执行此操作的简单方法。如果您使用的是最新的 jre,我认为是 7 及更高版本,则无需在 web.xml 文件中声明 servlet。@WebServlet("/servlet-url") 可以解决这个问题。

回答by Chetna R

function openPage(pageURL) {
            window.location = pageURL;
        }

In above code snippet, pageURL has to be an absolute URL which in case of dealing with servlets may vary.So instead of this below can be used

在上面的代码片段中,pageURL 必须是一个绝对 URL,在处理 servlet 的情况下可能会有所不同。因此可以使用下面的代替

location.href = (location.href).substr(0, (location.href).lastIndexOf('xyz.jsp'))+"/abc";

Here 'abc' is the servlet to which we have to redirect the 'xyz.jsp' .This can even work if there are many buttons.The respective functions could be written to redirect to respective servlets. Also it works in case of input type is "button" or "submit".

这里'abc'是我们必须将'xyz.jsp'重定向到的servlet。如果有很多按钮,这甚至可以工作。可以编写相应的函数来重定向到相应的servlet。它也适用于输入类型为“按钮”或“提交”的情况。