JSP 使用按钮类型将一个 jsp 页面指向另一个页面

JSP to direct one jsp page to another using button type

我需要通过按钮引导一个 jsp 页面 click.I 已经使用下面的代码尝试了这个场景我无法引导到那个页面。

<%--
  Created by IntelliJ IDEA.
  User: rajee
  Date: 2/15/15
  Time: 12:20 PM
  To change this template use File | Settings | File Templates.
--%>
<%@ 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">
<%@page import="java.sql.*"%>
<%
    try{
        String user=request.getParameter("user");
        String pass=request.getParameter("pass");
        Class.forName("com.mysql.jdbc.Driver").newInstance();
        Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/surveysample","root","root");
        Statement st=con.createStatement();
        ResultSet rs=st.executeQuery("select * from userdetail where name='"+user+"' and password='"+pass+"'");
        int count=0;
        while(rs.next()){
            count++;
        }
        if(count>0){
            out.println("welcome "+user);
        }
        else{
            response.sendRedirect("login.jsp");
        }
    }
    catch(Exception e){
        System.out.println(e);
    }
%>
<html>
<script>
</script>
<br>
<head>
    Summary Survey Details
</head>
<body>
    <h1>Recent Survey details</h1>
    <table border="3">
        <thead>survey details</thead>
        <th>Family  name</th>
        <th>First  name</th>
        <th>Middle  name</th>
        <th>Gender</th>
        <th>Birthday</th>
        <th>Income Group</th>
        <th>Complete Address</th>
        <th>Cordinates</th>
        <th>Mobile number</th>
        <th>email address</th>
        <th>Present Internet Provider</th>
        <th>Positive comments with present provider</th>
        <th>Negative remarks with present provider</th>
    </table>
    <a href="CreateSurvey.jsp">Create New Survey</a>
    <form name="SurveyMenu" action="SurveyMenu" method="get">
        <input type="button" value="Create new Survey" name="CreateSurvey" onclick="document.forms[0].action = 'CreateSurvey.jsp'; return true;" />
        &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp
        <input type="reset" value="Cancel" name="Cancel"  />
    </form>

</body>
</html>

你能解释一下为什么当我点击创建新调查按钮时没有发生这种情况吗?

这是我的登录代码

<%--
  Created by IntelliJ IDEA.
  User: rajee
  Date: 2/15/15
  Time: 12:18 PM
  To change this template use File | Settings | File Templates.
--%>
<%@ 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>
<script>
    function validate(){
        var username=document.form.user.value;
        var password=document.form.pass.value;
        if(username==""){
            alert("Enter Username!");
            return false;
        }
        if(password==""){
            alert("Enter Password!");
            return false;
        }
        return true;
    }
</script>
<form name="form" method="post" action="check.jsp" onsubmit="javascript:return validate();">
    <table>
        <tr><td>Username:</td><td><input type="text" name="user"></td></tr>
        <tr><td>Password:</td><td><input type="password" name="pass"></td></tr>
        <tr><td></td><td><input type="submit" value="Login"></td></tr>
        <tr><td></td><td><a href="register.jsp">Register Here</a></td></tr>
    </table>
</form>
</html>

要么,将 onclick 处理程序代码更改为

onclick="javascript:document.forms[0].action = 'CreateSurvey.jsp'; document.forms[0].submit();"

或者,将 <input> 类型更改为 submit as

<input type="submit" value="Create new Survey" name="CreateSurvey"
 onclick="javascript:document.forms[0].action = 'CreateSurvey.jsp'; return true;" />