【问题标题】:error requested source is not available after running jsp运行jsp后错误请求的源不可用
【发布时间】:2017-08-14 04:02:12
【问题描述】:

这将需要输入用户名和密码 将其传递给validation.jsp

login.jsp

<center>
            <table border="1" cellpadding="5" cellspacing="2">
                <thead>
                    <tr>
                        <th colspan="2">Login Here</th>
                    </tr>
                </thead>
                <tbody>
                    <tr>
                        <td>Username</td>
                        <td><input type="text" name="username" required/></td>
                    </tr>
                    <tr>
                        <td>Password</td>
                        <td><input type="password" name="password" required/></td>
                    </tr>
                    <tr>
                        <td colspan="2" align="center"><input type="submit" value="Login" />
                            &nbsp;&nbsp;
                            <input type="reset" value="Reset" />
                        </td>                        
                    </tr>                    
                </tbody>
            </table>
            </center>
        </form>
    </body>
</html>

validation.jsp
它将验证数据库中的数据并返回;

<%@ page import ="java.sql.*" %>
<%
    try{
        String username = request.getParameter("username");   
        String password = request.getParameter("password");
        Class.forName("com.mysql.jdbc.Driver");  // MySQL database connection
        Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/tejas?" + "user=root&password=tejas");    
        PreparedStatement pst = conn.prepareStatement("Select user,pass from login where user=tejas and pass=te65");
        pst.setString(1, username);
        pst.setString(2, password);
        ResultSet rs = pst.executeQuery();                        
        if(rs.next())           
           out.println("Valid login credentials");        
        else
           out.println("Invalid login credentials");            
   }
   catch(Exception e){       
       out.println("Something went wrong !! Please try again");       
   }      
%>`

这都是保存在 webapps 文件夹下的代码 webapps 文件夹包含 web-inf login.jsp validation.jsp

web.xml

<?xml version="1.0" encoding="ISO-8859-1"?>
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1"
metadata-complete="true">
<display-name>Welcome to Tomcat</display-name>
<description>
Welcome to Tomcat
</description>
</web-app>

我需要添加一些东西到 web.xml 请帮忙...

【问题讨论】:

  • jsp 用于显示目的,而不是数据库访问和业务逻辑。将此代码移动到 servlet 中
  • 您的提交按钮不会重定向到 validate.jsp

标签: java jsp tomcat servlets jdbc


【解决方案1】:

第一
你 setString 在你的查询中没有任何 ?,你必须使用,所以如果你想要一些动态的东西,你必须使用:

 pst = conn.prepareStatement("Select user,pass from login where user=? and pass=?");
 //------------------------------------------------------------------^----------^
 pst.setString(1, username);
 pst.setString(2, password);

如果您想修复用户名和密码,则无需使用.setString。

【讨论】:

  • 你做到了,但同样的错误,请求的源不可用
  • @TejasZambre 你能把e.printStackTrace(); 放在你的catch 中并告诉我们你的确切错误吗?
猜你喜欢
  • 1970-01-01
  • 2015-03-16
  • 2016-03-14
  • 1970-01-01
  • 2011-10-19
  • 2021-11-25
  • 1970-01-01
  • 2013-01-06
  • 1970-01-01
相关资源
最近更新 更多