在MyEclipse下创建servlet,代码如下:
package servlets;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.*;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.*;
public class LoginServlet extends HttpServlet {
private String name;
private String pass;
/**
* Constructor of the object.
*/
public LoginServlet() {
super();
}
/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.name=request.getParameter("account");
this.pass=request.getParameter("password");
PrintWriter out=response.getWriter();
//驱动程序名
String driverName="oracle.jdbc.driver.oracleDriver";
//数据库用户名
String userName="system";
//密码
String userPasswd="zmh";
//数据库名
String dbName="XE";
//表名
String tableName="lib_user";
//联结字符串
String url="jdbc:oracle:thin:@localhost:1512:XE";
try
{
Class.forName("oracle.jdbc.driver.oracleDriver");
Connection connection=DriverManager.getConnection(url);
Statement statement = connection.createStatement();
String sql="SELECT * FROM +lib_user";
ResultSet rs = statement.executeQuery(sql);
while(true)
{
if(rs.next())
{
if((this.name.equals(rs.getString(1)))&&(this.pass.equals(rs.getString(2))))
{
out.print("<h1>"+"Login Success!");
break;
}
}
else
{
out.print("<h1>"+"Account or Password Is Invalid!");
break;
}
}
return;
}
catch(SQLException e)
{
}
catch(ClassNotFoundException e)
{
}
}
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request,response);
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
// Put your code here
}
}
创建login.jsp代码如下:
<%@ page language="java" import="java.util.*" pageEncoding="GB2312"%>
Account: | |
Password: |
配置web.xml:
<?xml version="1.0" encoding="UTF-8"?>
LibraryService
A Login Servlet
LoginServlet
LoginServlet
servlets.LoginServlet
LoginServlet
/LoginServlet
login.jsp
在浏览器输入:http://localhost:8088/LibraryService/login.jsp能显示登录界面,但是输入账号和密码后显示如下:
HTtype Status report
message /LibraryService/Login
description The requested resource is not available.
Apache Tomcat/7.0.53TP Status 404 - /LibraryService/Login
求高手指教,本人刚接触这类软件,实在是找不出问题来,求指教~~~~~~~~~~~~