eclipse、sql2005、tomcat编写简单登陆页面,执行后一堆错误
我的环境是xp,32位eclipse,32位sql2005,tomcat7
在sql2005的数据库根节点下建了个叫demo1的数据库,里面建了叫Student的表,sql2005的登录名sa,密码123456
我写了两个页面,一个叫ceshi1.jsp,代码如下:
<html>
<head>
<script>
function checkform(){
var form1 = document.form1;
if(form1.username.value.length<1 || form1.username.value.length>20){
alert("用户名超出了范围(1~20).");
form1.username.focus();
return false;
}
if(form1.password.value.length<1 || form1.password.value.length>20){
alert("密码超出了范围(1~20).");
form1.password.focus();
return false;
}
}
</script>
<META content="MSHTML 6.00.2900.5848" name=GENERATOR>
</HEAD>
<BODY>
<form action="ceshi2.jsp" name="form1" id="form1">
<DIV id=div1>
<TABLE id=login height="100%" cellSpacing=0 cellPadding=0 width=800
align=center>
<TBODY>
<TR id=main>
<TD>
<TABLE height="100%" cellSpacing=0 cellPadding=0 width="100%">
<TBODY>
<TR>
<TD colSpan=4> </TD>
</TR>
<TR height=30>
<TD width=380> </TD>
<TD> </TD>
<TD> </TD>
<TD> </TD>
</TR>
<TR height=40>
<TD rowSpan=4> </TD>
<TD>用户名:</TD>
<TD>
<INPUT class=textbox id=username name=username>
</TD>
<TD width=120> </TD>
</TR>
<TR height=40>
<TD>密 码:</TD>
<TD>
<INPUT class=textbox id=password type=password
name=password>
</TD>
<TD width=120> </TD>
</TR>
<TR height=40>
<TD><br></TD>
<TD vAlign=center colSpan=2><br></TD>
</TR>
<TR height=40>
<TD></TD>
<TD align=right>
<INPUT id=btnLogin type=submit value=" 登 录 " name=btnLogin onclick="return checkform()">
</TD>
<TD width=120> </TD>
</TR>
<TR height=110>
<TD colSpan=4> </TD>
</TR>
</TBODY>
</TABLE>
</TD>
</TR>
<TR id=root height=104>
<TD> </TD>
</TR>
</TBODY>
</TABLE>
</DIV>
<DIV id=div2 style="DISPLAY: none"></DIV>
</CONTENTTEMPLATE>
</form>
</BODY>
</HTML>
第二个页面叫ceshi2.jsp,代码如下:
<%@ page import="java.sql.*" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%
String a=request.getParameter("username");
String b=request.getParameter("password");
Connection con;
Statement sql;
ResultSet rs;
try{Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");}
catch(Exception e){
out.print(e);}
try{String uri="jdbc:sqlserver://127.0.0.1:1433;DatabaseName=demo1";
String user="sa";
String password="123456";
con=DriverManager.getConnection(uri,user,password);
sql=con.createStatement();
rs=sql.executequery("select * from Student where name='"+a+"' and password='"+b+"'");
boolean m=rs.next();
if(m==true){
out.print("登陆成功!");
%>
<form action="ceshi1.jsp" method="post">
<IMPUT TYPE ="submit" value="退出登陆">
</form>}
<% else{out.print("登陆失败!");
%>
<form action="ceshi1.jsp" method="post">
<IMPUT TYPE ="submit" value="返回">
</form>}
}<%
catch(SQLException e){
out.print(e);
}
%>
</body>
</html>
执行ceshi1.jsp后,页面正常显示,输入Student表中有的名为admin,密码为admin后,网页就错误了,报错很多,如下,请问怎么改正?
type Exception report
message Unable to compile class for JSP:
description The server encountered an internal error that prevented it from fulfilling this request.
exception
org.apache.jasper.JasperException: Unable to compile class for JSP:
An error occurred at line: [37] in the generated java file: [C:\Documents and Settings\Administrator\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\work\Catalina\localhost\test1\org\apache\jsp\ceshi2_jsp.java]
Syntax error, insert "}" to complete ClassBody
An error occurred at line: 25 in the jsp file: /ceshi2.jsp
The method executequery(String) is undefined for the type Statement
22: String password="123456";
23: con=DriverManager.getConnection(uri,user,password);
24: sql=con.createStatement();
25: rs=sql.executequery("select * from Student where name='"+a+"' and password='"+b+"'");
26: boolean m=rs.next();
27: if(m==true){
28: out.print("xxxxxx!");
An error occurred at line: 29 in the jsp file: /ceshi2.jsp
Syntax error, insert "}" to complete Statement
26: boolean m=rs.next();
27: if(m==true){
28: out.print("xxxxxx!");
29: %>
30: <form action="ceshi1.jsp" method="post">
31: <IMPUT TYPE ="submit" value="xxxxx">
32: </form>}
An error occurred at line: 34 in the jsp file: /ceshi2.jsp
Syntax error, insert "}" to complete Block
31: <IMPUT TYPE ="submit" value="xxxx">
32: </form>}
33: <% else{out.print("xxxx!");
34: %>
35: <form action="ceshi1.jsp" method="post">
36: <IMPUT TYPE ="submit" value="xxx">
37: </form>}
An error occurred at line: 34 in the jsp file: /ceshi2.jsp
Syntax error, insert "}" to complete Block
31: <IMPUT TYPE ="submit" value="xxxx">
32: </form>}
33: <% else{out.print("xxx!");
34: %>
35: <form action="ceshi1.jsp" method="post">
36: <IMPUT TYPE ="submit" value="xx">
37: </form>}
An error occurred at line: [126] in the generated java file: [C:\Documents and Settings\Administrator\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\work\Catalina\localhost\test1\org\apache\jsp\ceshi2_jsp.java]
Syntax error on token "}", delete this token
Stacktrace:
org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:103)
org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:366)
org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:468)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:378)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:353)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:340)
org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:646)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:357)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
[解决办法]
楼主,你的java代码段和html,写串了。
格式化一下,好好看看括号的位置
错误的信息和位置都提示了
An error occurred at line: 34 in the jsp file: /ceshi2.jsp
Syntax error, insert "}" to complete Block
31: <IMPUT TYPE ="submit" value="xxxx">
32: </form>}
33: <% else{out.print("xxx!");
34: %>
35: <form action="ceshi1.jsp" method="post">
36: <IMPUT TYPE ="submit" value="xx">
37: </form> }