我有以下网络应用程序。 目标是在数据库连接后给出成功或失败消息。 在这个应用程序中,我有一个Login.jsp和loginHelper.java
请帮帮我。我需要这个帮助。
I hava a Login.JSP with below code.
-----------------------------------
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ page language="java"%>
<%@ page import="java.sql.*"%>
<%@ page import="com.test.loginHelper"%>
<%@ page import="java.util.*"%>
<%@ page import="java.text.*"%>
<%@ page import="javax.servlet.http.*"%>
<%
String user = request.getParameter("uname");
String pass = request.getParameter("pwd");
int stat;
//String dbURL = "jdbc:sqlserver://localhost:1433;databaseName=acc";
loginHelper tc = new loginHelper();
stat=tc.checkLogin(user,pass);
if (stat!= -1) {
%>
{"status":"success"}
<% }else{
%>
{"status":"failed"}
<%} %>
And i have one one Helper class with name loginHelper.java
package com.test;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
//import com.sun.org.apache.xalan.internal.utils.ObjectFactory;
public class loginHelper {
public int checkLogin(String uname,String pass) {
int status=-1;
Connection conn = null;
String dbName = "Test";
String serverip="localhost";
String serverport="1433";
String url = "jdbc:sqlserver://"+serverip+":"+serverport+";databaseName="+dbName+"";
String driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
String databaseUserName = uname;
String databasePassword = pass;
try {
Class.forName(driver).newInstance();
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
conn = DriverManager.getConnection(url, databaseUserName, databasePassword);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (conn!=null) {
status=1;
}
else
{
status=-1;
}
return status;
}
}
当我使用tomcat 7服务器配置在eclipse中运行它时。 在tomcat我收到505错误信息请帮帮我。
答案 0 :(得分:0)
网址中存在空格问题。因为你必须在传递连接之前通过编码url删除空格,如下所示。
String encodedURL=java.net.URLEncoder.encode(url,"UTF-8");
//Than pass it connection as below
DriverManager.getConnection(encodedURL, databaseUserName, databasePassword)