简体   繁体   中英

web service sample login using eclipse to create web service

I have below web application. Objective is to give success or failure message after database connection. In this application i am having one Login.jsp and loginHelper.java

Please help me. i need this help.

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;
    }
}

When i run this in eclipse with tomcat 7 server configuration. in tomcat i get 505 error message please help me out.

There has some issue with spaces in URL. As you have to remove spaces through encode url like below before passing into connection.

String  encodedURL=java.net.URLEncoder.encode(url,"UTF-8");
//Than pass it connection as below
DriverManager.getConnection(encodedURL, databaseUserName, databasePassword)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM