📄 firstservlet.java
字号:
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
public class firstservlet extends HttpServlet
{
static Connection con;
static String resulttosecond="NA";
public void doPost(HttpServletRequest req,HttpServletResponse res)throws ServletException,IOException
{
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con = DriverManager.getConnection("jdbc:odbc:MyDataSource","faculty","");
}
catch(Exception e)
{
System.out.println(e);
}
ServletContext context = getServletContext();
context.setAttribute("accountnumber","");
String accnum = req.getParameter("accnum");
String pinnum = req.getParameter("pinnum");
try{
PreparedStatement s = con.prepareStatement("select * from login where cAccount_Id=? and cPin_no=?");
s.setString(1,accnum);
s.setString(2,pinnum);
ResultSet rs = s.executeQuery();
boolean rowfound = false;
rowfound = rs.next();
if (rowfound == true)
{
resulttosecond = rs.getString(1);
context.setAttribute("accountnumber",resulttosecond);
RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/secondservlet");
if (dispatcher == null)
{
res.sendError(res.SC_NO_CONTENT);
}
dispatcher.forward(req,res);
try{
con.close();
}
catch(Exception e)
{
System.out.println(e);
}
}
if (rowfound == false)
{
PrintWriter out = res.getWriter();
res.setContentType("text/html");
resulttosecond = "NA";
out.println("<html>");
out.println("<body bgcolor=pink>");
out.println("Pls check the values that you have entered");
out.println("</body>");
out.println("</html>");
out.close();
}
}
catch(Exception e)
{
System.out.println(e);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -