我可以从html文件中获取参数;但我无法将这些参数发送到其他Servlet ..
我想要做的是,在一个servlet中获取用户名和密码,然后将这些值发送到另一个servlet进行身份验证,然后相应地重定向....
我是新手,我盲目地尝试了许多事情而无法完成工作....
这是我的第一个Servlet:
public void doGet(HttpServletRequest request,HttpServletResponse response) throws IOException,ServletException {
response.setContentType("text/html");
String uname=request.getParameter("uname");
String pass=request.getParameter("pass");
PrintWriter out=response.getWriter();
if(uname!=null && pass!=null){
ServletContext sc=getServletContext();
RequestDispatcher r=sc.getRequestDispatcher("/Authentication");
response.sendRedirect("Authentication");
} else {
out.println("Error");
}
这是我的第二个Servlet:
public void doPost(HttpServletRequest request,HttpServletResponse response) throws IOException,ServletException {
PrintWriter out = response.getWriter();
response.setContentType("text/html");
String uname=(String)request.getAttribute("uname");
String pass=(String)request.getAttribute("pass");
int p=pass.hashCode();
try{
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/aed","root","passwordmysql");
Statement st=con.createStatement();
ResultSet rs;
rs=st.executeQuery("select * from logids where userName='" + uname + "' and pass='" + p +"'");
if(rs.next()){
response.sendRedirect("home.html");
}else{
response.sendRedirect("Error.html");
}
}
catch(SQLException i){
i.printStackTrace();
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
我是新手,我很确定我犯了错误......
答案 0 :(得分:1)
从表单获取参数后,您需要将会话级别的属性设置为可供其他servlet使用。所以在发表声明之后:
String uname=request.getParameter("uname");
String pass=request.getParameter("pass");
添加以下内容:
request.getSession().setAttribute("uname", uname);
request.getSession().setAttribute("pass", pass);
然后在servlet2中,访问它:
String uname= (String)request.getSession().getAttribute("uname");
String pass= (String)request.getSession().getAttribute("pass");
答案 1 :(得分:0)
参阅文档:link
转发servlet允许您转发当前的servlet上下文 对象(它们的参数是转发的用户名,密码)
public void doGet(HttpServletRequest request,HttpServletResponse response) throws IOException,ServletException {
response.setContentType("text/html");
String uname=request.getParameter("uname");
String pass=request.getParameter("pass");
PrintWriter out=response.getWriter();
if(uname!=null && pass!=null){
ServletContext sc=getServletContext();
RequestDispatcher r=sc.getRequestDispatcher("/Authentication");
response.forward("Authentication");
} else {
out.println("Error");
}
第二个servlet
public void doPost(HttpServletRequest request,HttpServletResponse response) throws IOException,ServletException {
PrintWriter out = response.getWriter();
response.setContentType("text/html");
String uname=(String)request.getParameter("uname");
String pass=(String)request.getParameter("pass");
int p=pass.hashCode();
try{
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/aed","root","passwordmysql");
Statement st=con.createStatement();
ResultSet rs;
rs=st.executeQuery("select * from logids where userName='" + uname + "' and pass='" + p +"'");
if(rs.next()){
response.sendRedirect("home.html");
}else{
response.sendRedirect("Error.html");
}
}
catch(SQLException i){
i.printStackTrace();
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
注意:单独放置数据访问代码