当前位置:编程学习 > JAVA >>

servlet求助



 <display-name></display-name>
  <servlet>
    <description>This is the description of my J2EE component</description>
    <display-name>This is the display name of my J2EE component</display-name>
    <servlet-name>login</servlet-name>
    <servlet-class>view.LoginServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>login</servlet-name>
    <url-pattern>/LoginServlet</url-pattern>
  </servlet-mapping>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>


login.jsp内 form<form action="/com.AnswerSystem/view/LoginServlet" method="post" onSubmit="validate(this)">


不知道哪有错  求大神指出啊!! --------------------编程问答--------------------
这是 servlet


package view;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import model.*;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import control.DAOfactory;

import model.Student;

public class LoginServlet extends HttpServlet {

/**
 * 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 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 {


String userid = request.getParameter("userid");
String password = request.getParameter("password");
String user = request.getParameter("user");
List<String> info = new ArrayList<String>();
if(userid == null || "".equals(userid)){
info.add("用户名不能为空");
}
if(password == null || "".equals(password)){
info.add("密码不能为空");
}
if(info.size() == 0){
if(user.equals("student")){
Student student = new Student();
student.setS_ID(userid);
student.setPassword(password);
try{
if(DAOfactory.getUserDAOInstance().findLogin(student)){
info.add("欢迎学生"+student.getS_Name()+"光临");
request.setAttribute("info", info);
request.getRequestDispatcher("studentIndex.jsp").forward(request, response);

}else{
info.add("用户登录失败");
request.getRequestDispatcher("login.jsp").forward(request, response);
}
}catch(Exception e){e.printStackTrace();}
}

if(user.equals("teacher")){
Teacher teacher = new Teacher();
teacher.setT_ID(userid);
teacher.setPassword(password);
try{
if(DAOfactory.getUserDAOInstance().findLogin(teacher)){
info.add("欢迎老师"+teacher.getT_Name()+"光临");
request.setAttribute("info", info);
request.getRequestDispatcher("teacherIndex.jsp").forward(request, response);
}else{
info.add("用户登录失败");
request.getRequestDispatcher("login.jsp").forward(request, response);
}
}catch(Exception e){e.printStackTrace();}
}
if(user.equals("admin")){
Admin admin = new Admin();
admin.setA_ID(userid);
admin.setPassword(password);
try{
if(DAOfactory.getUserDAOInstance().findLogin(admin)){
info.add("欢迎管理员"+admin.getA_Name()+"光临");
request.setAttribute("info", info);
request.getRequestDispatcher("adminIndex.jsp").forward(request, response);
}else{
info.add("用户登录失败");
request.getRequestDispatcher("login.jsp").forward(request, response);
}
}catch(Exception e){e.printStackTrace();}
}

}
info.add("用户登录失败");
request.setAttribute("info", info);
request.getRequestDispatcher("login.jsp").forward(request, response);


}

/**
 * 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.doGet(request, response);
}

/**
 * Initialization of the servlet. <br>
 *
 * @throws ServletException if an error occurs
 */
public void init() throws ServletException {
// Put your code here
}

}
--------------------编程问答--------------------  <url-pattern>/LoginServlet</url-pattern>
这是URL


form里应该这样  :     action = "LoginServlet" --------------------编程问答-------------------- 从你form中的action看
<servlet-class>view.LoginServlet</servlet-class>应配置完整路径:包名.LoginServlet

另将action改为与<url-pattern>/LoginServlet</url-pattern>一致 --------------------编程问答-------------------- web.xml
 <servlet-class>com.AnswerSystem.view.LoginServlet</servlet-class>

login.jsp
<form action="<%=request.getContextPath()%>/LoginServlet" method="post" onSubmit="validate(this)"> --------------------编程问答-------------------- 这个好像应该放在web.xml里的吧 --------------------编程问答--------------------
引用 2 楼 starlight_520 的回复:
<url-pattern>/LoginServlet</url-pattern>
这是URL


form里应该这样  :     action = "LoginServlet"

好 --------------------编程问答-------------------- url应该是错了,应该没有工程名 --------------------编程问答-------------------- <form action="/com.AnswerSystem/view/LoginServlet" method="post" onSubmit="validate(this)">
上面的标记颜色的部分给去掉试试
你这个请求路径不对
怎么还有一个view/ --------------------编程问答-------------------- 路径在web.xml里面配置,而不是写全路径 --------------------编程问答-------------------- action后面添加的不是完整路径吧 --------------------编程问答-------------------- 3楼 6楼正解~~~~就是映射错了~~改过来就行了
补充:Java ,  Web 开发
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,