本人刚学习java,用angularjs+html+css做的前端,在编写登录系统的时候,发生了这样一个情况,后端我写了一个简单的关于登录验证的java文件,下面贴出源代码
package com.csii.action.login;
import java.sql.*;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class LoginAction extends HttpServlet{
static String USERNAME;
static String PASSWORD;
public static void main(String[] args) {
Connection conn=null;
Statement stmt=null;
String dbURL="jdbc:sqlserver://localhost:1433;DatabaseName=webuser";
String userName="sa";
String userPassword="123456";
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
conn=DriverManager.getConnection(dbURL,userName,userPassword);
System.out.println("数据库库连接成功");
System.out.println("创建数据库中。。。");
stmt=conn.createStatement();
String sql="select * from User_Info ";
ResultSet rs = stmt.executeQuery(sql);
ResultSetMetaData metaData = rs.getMetaData();
while(rs.next()){ //next()获取里面的内容
USERNAME=rs.getString(1);
PASSWORD=rs.getString(2);
}} catch (Exception e) {
e.printStackTrace();// TODO: handle exception
}
}
public String method1() {
String a=USERNAME;
return a;
}
public String method2() {
String b=PASSWORD;
return b;
}
public static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
doPost(req, resp);
}
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
String uname = req.getParameter("uname");
String pword = req.getParameter("pword");
PrintWriter pw = resp.getWriter();
String A=this.method1();
String B=this.method2();
System.out.println(A);
System.out.println(B);
if(A.equals(uname) && B.equals(pword))
{
pw.write("username and password is right!");
}else
{
pw.write("username or password is wrong!");
}
}
}
在这个java文件中一开始定义了一个全局变量USERNAME和PASSWORD,在第一个main方法中将这两个变量进行赋值,USERNAME=abcdefghi,PASSWORD=123456789,在main方法中可以打印出来USERNAME和PASSWORD的值,但是在doget和dopost方法中我是用System.out.println却只能输出空白行并且我在前端无论输入什么都是返回username or password is wrong!请各位帮帮我