站内搜索: 请输入搜索关键词

当前页面: 开发资料首页JSP 专题请问如何用Myeclipse开发Servlet?来者有分

请问如何用Myeclipse开发Servlet?来者有分

摘要: 请问如何用Myeclipse开发Servlet?来者有分


本人新手,遇到一些问题,请各位朋友指点,谢谢!今天用myeclipse做用户登录验证页面,login.html是提示用户输入用户名和密 码,LoginServlet.java用来接收login.html的用户名和密码.这两个文件分别存放在:
D:/hp/j2ee/userlogin/WebRoot
D:/hp/j2ee/userlogin/WebRoot/WEB-INF/classes
web.xml是myeclipse自动生成的:代码如下

<?xml version="1.0" encoding="UTF-8"?>
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

This is the description of my J2EE component
This is the display name of my J2EE component
LoginServlet
LoginServlet


LoginServlet
/servlet/LoginServlet



login.html的代码如下:
HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<head>
login.html


</head>
<body>

<form method="post" name="frmLogin" action="LoginServlet">
<table align="center" bgcolor="#cccccc" border="0" cellPadding="0" cellSpacing="0" width="273">
<tr>
<td colspan="2" align=center height="32">

用户登录

</td>
</tr>
<td align="right" height="47" valign="bottom" width="94">用户名:
</td>
<td height="47" valign="bottom" width="172">
<input name="txtUserName" size="20" maxlength="20">
</td>
<tr>
<td align="right" width="94">密码:
</td>
<td width="172">
<input name="txtPassword" type="password" size="20" maxlength="20">
</td>
</tr>
<tr>
<td align="right" heigth"44" width="94">
</td>
<td height="44" width="172">
<input name="Submit" type="submit" value="提交">
&nbsp;&nbsp;&nbsp;
<input name="Reset" type="reset" value="重置">
</td>
</tr>
</table>
</form>

</body>

LoginServlet的代码如下:

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.*;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class LoginServlet extends HttpServlet
{
public LoginServlet()
{
super();
}
public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException
{

}
public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException
{
response.setContentType("text/html");
String result="";
String strUserName=request.getParameter("txtUserName");
String strPassword=request.getParameter("txtPassword");
if((strUserName==null)||(strUserName.equals(""))||(strUserName.length()>20))
{
result="请输入用户名(不超过20字符)";
request.setAttribute("error_username",result);
response.sendRedirect("login2.jsp");
}
if((strPassword==null)||(strPassword.equals(""))||(strPassword.length()>20))
{
Connection conn=null;
ResultSet rs=null;
Statement stmt=null;
result="请输入密码(不超过20字符)";
request.setAttribute("error_password",result);
response.sendRedirect("login2.jsp");
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
conn=DriverManager.getConnection("jdbc:odbc:bb","","");
stmt=conn.createStatement();
String sql="select * from users where username='"+strUserName+"',password='"+strPassword+"'";
rs=stmt.executeQuery(sql);
if(rs.next())
{
request.getSession(true).setAttribute("userName", strUserName);
response.sendRedirect("login_success.jsp");
}
else
{
response.sendRedirect("login_failure.jsp");
}
}
catch(SQLException sqlExc)
{
sqlExc.printStackTrace();
}
catch(Exception e)
{
e.printStackTrace();
}
finally
{
try
{
if(rs!=null)
{
rs.close();
}
if(stmt!=null)
{
stmt.close();
}
}
catch(SQLException sqlEx)
{
sqlEx.printStackTrace();
}
}
}
}
}



为什么一样的代码。
if((strUserName==null)||(strUserName.equals(""))||(strUserName.length()>20))
{
result="请输入用户名(不超过20字符)";
request.setAttribute("error_username",result);
response.sendRedirect("login2.jsp");
}
if((strPassword==null)||(strPassword.equals(""))||(strPassword.length()>20))
{
Connection conn=null;
ResultSet rs=null;
Statement stmt=null;
result="请输入密码(不超过20字符)";
request.setAttribute("error_password",result);
response.sendRedirect("login2.jsp");
try


这两个分别用于判断用户名和密码是否超过20位


忘了说了,程序出错是因为找不到servlet
The requested resource (/userlogin/LoginServlet) is not available


大家帮帮忙啊,谢谢了


LoginServlet是在哪个包里?你自己有定义的包么?


LoginServlet没有包,生成的class文件就在classes文件中


问题解决了

<form method="post" name="frmLogin" action="LoginServlet">
改为
<form method="post" name="frmLogin" action="/userlogin/LoginServlet">


接分


up !
接分^_^


jf 3q


接分


↑返回目录
前一篇: 最后20分问个问题
后一篇: 如何利用CSS中的ime-mode用来控制页面上文本框中的全角/半角输入?(在线等待)