我为登录界面写了一个简单的java代码
这是代码:
Login.java
package Banking;
import java.io.*;
import javax.servlet.*;
import java.sql.*;
import javax.servlet.http.*;
public class Client extends HttpServlet
{
Connection con;
String s3="",s4="";
public void init(ServletConfig sc)throws ServletException
{
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection("jdbc:odbc:thin","sys","orcl as sysdba");
}catch(Exception ee){System.out.println(ee);}
}
public void doGet(HttpServletRequest req,HttpServletResponse res)throws ServletException,IOException
{
PrintWriter pw=res.getWriter();
res.setContentType("text/html");
String s1=req.getParameter("user");
String s2=req.getParameter("pwd");
try{
Statement st=con.createStatement();
ResultSet rs=st.executeQuery("select name from pass where name=' "+s1+" ' ");
while(rs.next())
{
s3=rs.getString(1);
s4=rs.getString(2);
}
if(s1.equals("")||s2.equals(""))
{
pw.println("invalid");
res.sendRedirect("http://localhost:8080p/tball.gif");
}
else
{
PreparedStatement pst=con.prepareStatement("insert into pass values(?,?)" );
pst.setString(1,s1);
pst.setString(2,s2);
pst.execute();
res.sendRedirect("http://localhost:8080/welcome.html");
}
}catch(Exception e){res.sendRedirect("http://localhost:8080/invalid.html");}
}
}
Data.java
package Banking;
import java.io.*;
import javax.servlet.*;
import java.sql.*;
import javax.servlet.http.*;
public class Client2 extends GenericServlet
{
Connection con;
public void init(ServletConfig sc)throws ServletException
{
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection("jdbc:oracle:thin","sys","orcl as sysdba");
}catch(Exception ee){System.out.println(ee);}
}
public void service(ServletRequest req,ServletResponse res)throws ServletException,IOException
{ boolean j=true;
String s1=req.getParameter("t1");
String s2=req.getParameter("t2");
String s3=req.getParameter("t3");
String s4=req.getParameter("r");
String s5=req.getParameter("s");
String s6=req.getParameter("t4");
String s7=req.getParameter("t5");
String s8=req.getParameter("t6");
String s9=req.getParameter("s1");
String s10=req.getParameter("t7");
String s11=req.getParameter("t8");
String s12=req.getParameter("t9");
try{
PreparedStatement pst=con.prepareStatement("insert into entry values(?,?,?,?,?,?,?,?,?,?,?,?)" );
pst.setString(1,s1);pst.setString(2,s2);pst.setString(3,s3);pst.setString(4,s4);
pst.setString(5,s5);pst.setString(6,s6);pst.setString(7,s7);pst.setString(8,s8);
pst.setString(9,s9);pst.setString(10,s10);pst.setString(11,s11);pst.setString(12,s12);
pst.execute();
}
catch(Exception e)
{System.out.println(e);}
PrintWriter pw=res.getWriter();
res.setContentType("text/html");
pw.println("inserted.into table..");
}
}
我收到了很多错误:
GenericServlet cannot be resolved to a type data.java /Myproject/src/Banking line 8 Java Problem
HttpServlet cannot be resolved to a type Login.java /Myproject/src/Banking line 7 Java Problem
HttpServlet cannot be resolved to a type login.java /banking/src/banking line 9 Java Problem
HttpServletRequest cannot be resolved to a type Login.java /Myproject/src/Banking line 20 Java Problem
HttpServletRequest cannot be resolved to a type login.java /banking/src/banking line 22 Java Problem
HttpServletResponse cannot be resolved to a type Login.java /Myproject/src/Banking line 20 Java Problem
HttpServletResponse cannot be resolved to a type login.java /banking/src/banking line 22 Java Problem
ServletConfig cannot be resolved to a type Login.java /Myproject/src/Banking line 12 Java Problem
ServletConfig cannot be resolved to a type data.java /Myproject/src/Banking line 11 Java Problem
ServletConfig cannot be resolved to a type login.java /banking/src/banking line 14 Java Problem
ServletException cannot be resolved to a type Login.java /Myproject/src/Banking line 12 Java Problem
ServletException cannot be resolved to a type Login.java /Myproject/src/Banking line 20 Java Problem
ServletException cannot be resolved to a type data.java /Myproject/src/Banking line 11 Java Problem
ServletException cannot be resolved to a type data.java /Myproject/src/Banking line 19 Java Problem
ServletException cannot be resolved to a type login.java /banking/src/banking line 14 Java Problem
ServletException cannot be resolved to a type login.java /banking/src/banking line 22 Java Problem
ServletRequest cannot be resolved to a type data.java /Myproject/src/Banking line 19 Java Problem
ServletResponse cannot be resolved to a type data.java /Myproject/src/Banking line 19 Java Problem
The import javax.servlet cannot be resolved Login.java /Myproject/src/Banking line 3 Java Problem
The import javax.servlet cannot be resolved Login.java /Myproject/src/Banking line 5 Java Problem
The import javax.servlet cannot be resolved data.java /Myproject/src/Banking line 4 Java Problem
The import javax.servlet cannot be resolved data.java /Myproject/src/Banking line 6 Java Problem
The import javax.servlet cannot be resolved login.java /banking/src/banking line 4 Java Problem
The import javax.servlet cannot be resolved login.java /banking/src/banking line 6 Java Problem
The public type Client must be defined in its own file Login.java /Myproject/src/Banking line 7 Java Problem
The public type Client2 must be defined in its own file data.java /Myproject/src/Banking line 8 Java Problem
如何解决这些错误?
答案 0 :(得分:1)
你确定添加了libs吗?检查构建路径。
答案 1 :(得分:1)
您需要按以下方式命名您的课程:
public class Login extends HttpServlet
{
// you
}
public class Data extends GenericServlet
{
// .... you code here
}
因为您的文件名为Login.java
和Data.java