考试题型
单选题20道(1分)
多选题10道(2分),多选题选到部分答案得一半分,
填空题十个空(2分)
编程题或者程序填空题40分
连接数据库
import java.sql.*;
public class MIKA{
//连接数据库
private final static String Driver="com.microsoft.sqlserver.jdbc.SQLServerDriver";//数据库驱动Driver
private finalstatic String Url="jdbc:sqlserver://localhost:1433;DatabaseName=mika";
//登录数据库的账号
private final static String userid="sa";
//登录数据库的密码
private final static String pwd="123456";
public Connection getConn(){
Connection conn=null;
try{
Class.forName("Driver");//加载数据库驱动
conn=DriverManager.getConnection(Url,userid,pwd);//建立连接
}catch(Exception e){
System.out.println("驱动加载失败!");
e.printStackTrace();
}
Return conn;
}
public void closeAll(Connection conn){
try{
if(conn!=null){
Conn.close();
}catch(Exception e){
System.out.println("conn关闭失败");
e.printStackTrace();
}
}
}
servlet编写计算圆面积和圆周长
public class request extends HttpServlet{
public void doGet(HttpServletRequest request,HttpServletResponse response)throws IOException,ServletException
{
response.setContentType("text/html;charset=GBK");
String title="计算圆面积和周长";
PrintWriter out=response.getWriter();
out.println("<html>");//HTML格式
out.println("<body>");
out.println("<head>");
out.println("<title>"+title+"<title>");
out.println("</head>");
out.println("</body>");
out.println("<h3>"+title+"</h3>");
String radius=request.getParameter("radius");
//从页面获取数据
if(radius!=null)
{
float r=(new Float(radius)).floatValue();
double s=2*3.14*r;
double area=3.14*r*r;
out.println("圆的周长:"+s+"<br>");
out.println("圆的面积:"+area);
}//如果获取的数据不为空值
else{
out.println("您还未提交!");
}
}
}
使用Servlet登录
NewFile.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="../loginServlet" method="get">
<label>
用户名:<input type="text" placeholder="用户名" name="username"><br>
密码:<input type="text" placeholder="密码" name="password"><br>
</label>
<button type="submit">提交</button>
</form>
</body>
</html>
package servlet;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet("/failServlet")
public class FailServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setContentType("text/html;charset=utf-8");
resp.getWriter().write("登录失败!");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doGet(req,resp);
}
}
package servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.catalina.User;
@WebServlet("/loginServlet")
public class LoginServlet extends HttpServlet{
protected void doGet(HttpServletRequest req,HttpServletResponse resp)throws ServletException,IOException{
req.setCharacterEncoding("utf-8");//设定字符编码格式为utf-8
String username=req.getParameter("username");//获取表单提交的username数据
String password=req.getParameter("password");//获取密码
User user=new User();
user.setUsername(username);
user.setPassword(password);
UserLogin ul=new UserLogin();
User login=ul.login(user);
if(login==null) {
req.getRequestDispatcher("/failServlet").forward(req, resp);
}else {
req.setAttribute("user", user);
req.getRequestDispatcher("/successServlet").forward(req, resp);
}
}
protected void doPost(HttpServletRequest req,HttpServletResponse resp)throws ServletException,IOException{
this.doGet(req, resp);
}
}
package servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/successServlet")
public class SuccessServlet extends HttpServlet{
protected void doGet(HttpServletRequest req,HttpServletResponse resp)throws ServletException,IOException{
User user=(User)req.getAttribute("user");
if(user!=null) {
resp.setContentType("text/html;charset=utf-8");
resp.getWriter().write(user.getUsername()+"登陆成功");
}
}
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doGet(req,resp);
}
}
package servlet;
public class User {
private int id;
private String username;
private String password;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
package servlet;
public class UserLogin {
public User login(User loginUser) {
if(loginUser.getUsername().equals("mika")&&loginUser.getPassword().equals("123"))
return loginUser;
else return null;
//如果提交的数据符合账号为mika密码为123 则判断正确登录
}
}
|