当前位置: 技术问答>java相关
为什么我的servlet用不了?
来源: 互联网 发布时间:2015-07-25
本文导语: 我试着学习servlet,但是出现了一个问题,我想做一个登陆程序, index.jsp代码如下: 登陆 你的姓名: 你的密码: 我的logon.java代码如下: package Myservlet; import javax.servlet.*; imp...
我试着学习servlet,但是出现了一个问题,我想做一个登陆程序,
index.jsp代码如下:
登陆
你的姓名:
你的密码:
我的logon.java代码如下:
package Myservlet;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import java.sql.*;
//port java.lang.*;
//import java.net.ContentHandler.*;
public class logon extends HttpServlet
{
private static final String CONTENT_TYPE = "text/html;charset=gb2312";
public void init() throws ServletException
{
String sDBDriver = "weblogic.jdbc.oci.Driver";
try
{
Class.forName(sDBDriver);
}
catch(java.lang.ClassNotFoundException e)
{
System.err.println(e.getMessage());
}
}
public ResultSet executeQuery(String sql)
{
String sConnStr = "jdbc:weblogic:oracle";
Connection connect = null;
ResultSet rs = null;
try
{
connect = DriverManager.getConnection(sConnStr);
Statement stmt = connect.createStatement();
rs = stmt.executeQuery(sql);
}
catch(SQLException ex)
{
System.err.println(ex.getMessage());
}
return rs;
}
public String getStr(String str)
{
try
{
String temp_p = str;
byte[] temp_t = temp_p.getBytes("ISO8859-1");
String temp = new String(temp_t);
return temp;
}
catch(Exception e)
{
e.printStackTrace();
}
return null;
}
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
response.setContentType(CONTENT_TYPE);
PrintWriter out = response.getWriter();
out.println("");
out.println("
out.println("");
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
response.setContentType(CONTENT_TYPE);
PrintWriter out = response.getWriter();
out.println("");
out.println("login");
out.println("");
out.println("");
String logname, logpass;
boolean loginAttempt = false;
boolean loginOK = false;
String errorMessage = "请您登陆";
HttpSession session = request.getSession(true);
if(request.getParameterValues("logon") != null
&&request.getParameterValues("name") != null
&&request.getParameterValues("password") != null)
{
loginAttempt = true;
}
if (loginAttempt)
{
logname=request.getParameter("name");
logpass=request.getParameter("pass");
logname=getStr(logname);
logpass=getStr(logpass);
String sql="select * from logoninfo where name='"+logname+"' and password='"+logpass+"'";
out.println(sql);
ResultSet RS=executeQuery(sql);
int rowscount=0;
try
{
while(RS.next())
{
rowscount++;
}
}
catch(Exception e)
{
e.printStackTrace();
}
//count.........
out.println(rowscount);
if(rowscount!=0)
{
errorMessage="成功登陆";
//session.setAttribute("username", logname);
loginOK=true;
if(loginOK)
{
response.sendRedirect("/instance1/main.jsp");
}
}
else
{
errorMessage = "loginerr";
//session.setAttribute("username", "");
response.sendRedirect("/instance1/logon.jsp?err="+errorMessage);
}
}
}
public void destroy()
{
}
}
index.jsp代码如下:
登陆
你的姓名:
你的密码:
我的logon.java代码如下:
package Myservlet;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import java.sql.*;
//port java.lang.*;
//import java.net.ContentHandler.*;
public class logon extends HttpServlet
{
private static final String CONTENT_TYPE = "text/html;charset=gb2312";
public void init() throws ServletException
{
String sDBDriver = "weblogic.jdbc.oci.Driver";
try
{
Class.forName(sDBDriver);
}
catch(java.lang.ClassNotFoundException e)
{
System.err.println(e.getMessage());
}
}
public ResultSet executeQuery(String sql)
{
String sConnStr = "jdbc:weblogic:oracle";
Connection connect = null;
ResultSet rs = null;
try
{
connect = DriverManager.getConnection(sConnStr);
Statement stmt = connect.createStatement();
rs = stmt.executeQuery(sql);
}
catch(SQLException ex)
{
System.err.println(ex.getMessage());
}
return rs;
}
public String getStr(String str)
{
try
{
String temp_p = str;
byte[] temp_t = temp_p.getBytes("ISO8859-1");
String temp = new String(temp_t);
return temp;
}
catch(Exception e)
{
e.printStackTrace();
}
return null;
}
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
response.setContentType(CONTENT_TYPE);
PrintWriter out = response.getWriter();
out.println("");
out.println("
The servlet get method is the reply.
");out.println("");
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
response.setContentType(CONTENT_TYPE);
PrintWriter out = response.getWriter();
out.println("");
out.println("login");
out.println("");
out.println("");
String logname, logpass;
boolean loginAttempt = false;
boolean loginOK = false;
String errorMessage = "请您登陆";
HttpSession session = request.getSession(true);
if(request.getParameterValues("logon") != null
&&request.getParameterValues("name") != null
&&request.getParameterValues("password") != null)
{
loginAttempt = true;
}
if (loginAttempt)
{
logname=request.getParameter("name");
logpass=request.getParameter("pass");
logname=getStr(logname);
logpass=getStr(logpass);
String sql="select * from logoninfo where name='"+logname+"' and password='"+logpass+"'";
out.println(sql);
ResultSet RS=executeQuery(sql);
int rowscount=0;
try
{
while(RS.next())
{
rowscount++;
}
}
catch(Exception e)
{
e.printStackTrace();
}
//count.........
out.println(rowscount);
if(rowscount!=0)
{
errorMessage="成功登陆";
//session.setAttribute("username", logname);
loginOK=true;
if(loginOK)
{
response.sendRedirect("/instance1/main.jsp");
}
}
else
{
errorMessage = "loginerr";
//session.setAttribute("username", "");
response.sendRedirect("/instance1/logon.jsp?err="+errorMessage);
}
}
}
public void destroy()
{
}
}
|
package Myservlet;
就是说你应该把logon.class放到classesMyservlet目录下面
就是说你应该把logon.class放到classesMyservlet目录下面
|
用servlet除了要把你编译好的servlet 的class 文件拷贝到WEB-INF目录下的classes 目录下,主要还得编辑WEB-INF目录下的web.xml文件,把你servlet部署上去才行。
如:
BBSServlet
com.moia.bbs.BBSServlet
BBSServlet
BBSServlet
如:
BBSServlet
com.moia.bbs.BBSServlet
BBSServlet
BBSServlet
|
出现什么问题?